The component state updates are not as simple as they seem, they are asynchronous, in short it takes a while to happen and the variables with the new states need to be monitored to make some decision, in their code basically to work:
function Card() {
const [number1, setNumber1] = React.useState(0);
const [number2, setNumber2] = React.useState(0);
const [operation, setOperation] = React.useState("");
const [result, setResult] = React.useState(0);
function random() {
return parseInt(Math.random() * (11 - 0) + 0);
}
React.useEffect(() => {
setOperation(`${number1}+${number2}`);
},[number1, number2]);
React.useEffect(() => {
setResult(state => number1 + number2);
}, [operation]);
function handleSubmit() {
setNumber1(state => random());
setNumber2(state => random());
}
return (
<div className="card">
<h1>Treino de cálculo</h1>
<h2>
{operation} = {result}
</h2>
<input type="number" placeholder="Sua resposta" />
<button onClick={() => handleSubmit()}>
Verificar resposta
</button>
</div>
);
}
ReactDOM.render( <Card/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root">Carregando ...</div>
in this example waits to change the state of the two variables: number1
and number2
then update the variable operation
and finally show all the results, IE, has a logical sequence of update to work.
There is a way much better and for me more reliable, example:
function Card() {
const [data, setData] = React.useState({
number1: 0,
number2: 0
});
const [result, setResult] = React.useState({
operation: '',
result: 0
});
function random() {
return parseInt(Math.random() * (11 - 0) + 0);
}
function handleSubmit() {
const number1 = random();
const number2 = random();
setData({number1, number2});
}
React.useEffect(() => {
const { number1, number2 } = data;
const operation = `${number1} + ${number2}`;
const result = number1 + number2;
setResult({
operation,
result
});
}, [data]);
return (
<div className="card">
<h1>Treino de cálculo</h1>
<h2>
{result.operation} = {result.result}
</h2>
<input type="number" placeholder="Sua resposta" />
<button onClick={() => handleSubmit()}>
Verificar resposta
</button>
</div>
);
}
ReactDOM.render( <Card/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root">Carregando ...</div>
in this second form is created an object and when this object is changed, I change the result object is easier to control the changes and sequence of the state variables.
Thanks Virgilio, as I am still learning There are things there that I have no idea what hahah is, but I will take a good look and study the code. When complete I will put on my github :D. Thank you very much!
– Yago Juan
@Yagojuan if useful tick as answer !
– novic
Well again I received a negative vote, I do not know the reasons that led to this, I believe it is persecution. I do not know what to do!
– novic
Virgilio, I got it with your first code, I just had to make a few minor changes to fit better into my project. Thank you so much for your help. I marked it as a response
– Yago Juan