Skip to content
Advertisement

Update one element of State React

I have a problem, a bit hard to explain.

I use Hooks to set a state, from the data I receive from my API. I display it on my frontend, no problem.

I would like to update ONE value, from ONE element.

Here is my full component :

JavaScript

So, when I click on my <p>, it transforms to an input. Then I would like to modify the amount of income, and set it to the state.

For information, my collection (mongo) look like this :

JavaScript

So I need to update only the income category, and only the amount from the key selected. I don’t know if I’m clear…

Any idea ?

Additional info : My setUserWalletIncomes in the input erase all the state, and replace it with the only value from the input.

Advertisement

Answer

You can take advantage of object destructuring to do that

JavaScript

See the MDN docs for more info on the matter.

Edit: You already have it thanks to your Object.entries func. See below for full code.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement