Skip to content
Advertisement

ReactJs : Nested State Not getting Updated

I have below code :

JavaScript

In this, iState has Error portion nested within –

JavaScript

When I am trying to update the nested state of Error , its not getting updated –

JavaScript

I can see it entering within else block , but not updating the state. I also tried with – setstate({...state.Error, EName:''})

EDIT 1 :

JavaScript

Setting state changed –setstate({ ...state, Error: { ...state.Error, Ename: 'Value Cannot be blank' }})

Still state not getting updated –

enter image description here

Advertisement

Answer

Unlike this.setState in class components, state setter function returned by the useState hook does not merges the old state with the new state – you need to do it yourself. Not doing this will overwrite the existing state.

Currently you are overwriting the existing state. Correct way to update the state is shown below:

This

JavaScript

should be

JavaScript

Explanation of why this works:

First you spread the top level state object in the newly created object passed to setState. After that you add Error key and its value is another object in which you spread state.Error. Finally, you add a Ename key in the nested object and set its value.

Above steps re-create a new object with the similar structure as the initial state object.

Similarly this

JavaScript

should be

JavaScript

Edit

Please note that state is updated asynchronously and it is constant within a particular render of a component.

This means that logging the state immediately after calling the state setter function will log the old state. Component can only see the updated state after it has re-rendered.

To log the updated state, use the useEffect hook as shown below:

JavaScript

This will log the updated state because it will execute:

  • after the initial render
  • every time component re-renders as a result of state update.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement