Skip to content
Advertisement

Update Nested Objects in React Native using setState

I am trying to change the properties of objects inside of an object and trying to add new properties to these objects but keeping the old values.

I can’t find out how to get the right nested object by index, not id because the id can be different from the .map index.

This is what I got so far, the Object names are for testing purposes only and maybe the “updateNestedObject” can be run in the parent?

Thank you in advance and sorry if this is a noob question.

Neval

JavaScript

Advertisement

Answer

There were few issues:

  1. JSX component name editNested should start with a capital letter.

  2. And editNested component should be on its own function, should not define inside another component which caused your TextInput to lose focus after each render cycle.

  3. The setState call should be changed like below:

JavaScript

Try the code below:

JavaScript

Working Demo

Edit goofy-river-uy5z9e

Advertisement