Skip to content
Advertisement

React native: Update value of object in array in state

I have a component which changes the state when checkbox is checked and the data needs to be updated of the object in the array.

The component state looks something like this { key:1, todo:”Something”, isChecked:false }

i have 3 files: AddTodo.js Which passes state & setState to an component TodoList which passes it the subcomponent TodoItem.

I am unable to update the state from TodoItem , I need to implement a function that finds the object from array and updates its isChecked state.

AddTodo.js

JavaScript

TodoList.js

JavaScript

TodoItem.js

JavaScript

Advertisement

Answer

JavaScript

and in your todo item

JavaScript

i also don’t think that you need an is checked state in your todo component since you are passing that through props (so delete const [checked, setCheck] = useState(props.isChecked) line and just use the value you are getting from props.isChecked)

didn’t pay much attention to your variable names but this should put you on the right track

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