score:0

i'm not sure why, but this is how it goes:

// formvalues === 1
setformvalues(2);
// formvalues === 1

i think it's related to the way react works under the hood, or whatever. anyway, one would never update the state the way you did. this is how you can retrieve current value:

// formvalues === 1
setformvalues(2);
// formvalues === 1
setformvalues(actualvalue => {
    // actualvalue === 2
    return 3;
});

alternatively, just set the whole array at once.

this should do it:

setformvalues(actualvalues => [...actualvalues, { name: name, id: id }]);

Related Query

More Query from same tag