0
I’m trying to put items in a array
by clicking the button, but the same one is starting empty. Notice that I clicked 3 times but it shows me that the array
has 2 positions.
I have a div
which is the display
that one div
receives the state of the constant operation
which is an array. Clicking on a button triggers a function called addOperation(2)
with the value 2, which places this number inside the array.I am using setOperation([...operation, value])
to push the number on array
.
The problem is that by clicking the button the program puts the number in the second position of the array, and the first one is empty, you know what may be happening?
The state update is an asynchronous operation. One of the ways to get the updated value is to use useEffect. Here are some examples: https://reactjs.org/docs/hooks-effect.html
– Bins
An important thing, do not put the image, only in certain case, for example that is code, always put the code.
– novic
One thing if my answer was the solution of your problem accepted as answer and take a look at the tour so you know how it works.
– novic