const Logic = () => {
const [inputValues, setInputValues] = useState('');
console.log('1)', inputValues);
const onChangeInput = e => {
setInputValues((currentState) => {
console.log('2) current', currentState);
return e.target.value});
console.log('3)', inputValues);
};
return <View inputValues={inputValues} onChangeInput={onChangeInput} />;
};
// Будем вводить букву 'a' в инпут. В итоге в консоле получим:
// 1)"" <-- из-за useState('');
// 2) current "" <-- так как значение мы ещё не обновили, то получаем то же начальное значение из useState('');
// 3) "" <-- вызвывается ПОСЛЕ setInputValues, тем не менее, имеет ещё не обновлённое значение.
// 1) a <-- произошло изменение состояния, компонент перерисовался.
void func1(string s) // будет работать в Java быстрее, чем в С++
class Bla {
customNameMethod() {
alert()
}
}
const bla = new Bla();
bla["custom" + `Name` + 'Method']();
let methodName = `Name` + 'Method';
bla[`custom${methodName}`]();
renderNoResultsMessage() {
const { results, noResultsMessage } = this.props;
if (results && !results.length && noResultsMessage) {
return (<div className="no-result">
<h3>
{noResultsMessage}
</h3>
</div>);
}
}
const attacks = [
{ minChance: 7, damage: 40, name: 'critical' },
{ minChance: 5, damage: 20, name: 'big' },
{ minChance: 0, damage: 10, name: 'weak' },
];
const messages = {
start: (player, enemy) => `Welcome! Yor health is - ${player}%, your enemy health - ${enemy}%`,
end: (player, enemy) => `You ${enemy <= 0 ? 'win' : 'lost'}! Your hp level - ${player}, opponents hp level - ${enemy}`,
chance: (player, enemy) => `your chance - ${player}, your opponent chance - ${enemy}`,
turn: (player, enemy, hit, isEnemy) =>
`${isEnemy ? 'Enemy' : 'Your'} turn...
${isEnemy ? 'Enemy' : 'You'} did a ${hit} hit
${isEnemy ? 'Your' : 'Enemy'} hp - ${isEnemy ? player : enemy}`,
};
const simpleFight = () => {
const hp = [ 100, 100 ];
console.log(messages.start(...hp));
while (hp.every(n => n > 0)) {
const chances = hp.map(() => Math.random() * 11 | 0);
console.log(messages.chance(...chances));
if (chances[0] !== chances[1]) {
const chance = Math.max(...chances);
const attack = attacks.find(n => n.minChance < chance);
const isEnemyAttacks = chance === chances[1];
hp[+!isEnemyAttacks] -= attack.damage;
console.log(messages.turn(...hp, attack.name, isEnemyAttacks));
}
}
console.log(messages.end(...hp));
};
const { id } = this.state;
const { id } = this.state;
const newOrder = { id, ... };
const orders = [];