0

I am trying to update the property of an object which is stored in an array.

my state looks something like this:

state = {
  todos: [
    {
     id: '1',
     title: 'first item,
     completed: false
    },
    {
     id: '2',
     title: 'second item,
     completed: false
    }
  ],
}

What I am trying to do is access the second element in the 'todos' array and update the completed property to either false -> true or true -> false.

I have a button with the handler for update, and my class method for the update looks like this:

onUpdate = (id) => {
  const { todos } = this.state;
  let i = todos.findIndex(todo => todo.id === id);
  let status = todos[i].completed
  let updatedTodo = {
    ...todos[i],
    completed: !status
  }
  this.setState({
    todos: [
      ...todos.slice(0, i),
      updatedTodo,
      ...todos.slice(i + 1)
    ]
  });
}

While this does work, I want to find out if there is a more concise way of achieving the same result; I tried to use Object.assign(), but that didn't work out because my 'todos' is an array, not an object. Please enlighten me with better code!

2 Answers 2

1

It would be best to use update function to make sure you don't work on outdated data:

onUpdate = (id) => {
  this.setState(prevState => {
    const copy = [...prevState.todos];
    const index = copy.findIndex(t => t.id === id);
    copy[index].completed = !copy[index].completed;

    return { todos: copy }
  })  
}
Sign up to request clarification or add additional context in comments.

1 Comment

just tried this and it works like a charm! Thank you.
1

You can simply copy your todos from state, then make edits, and after that put it back to the state

onUpdate = (id) => {
    var todos = [...this.state.todos]
    var target = todos.find(todo => todo.id == id)
    if (target) {
        target.completed = !target.completed
        this.setState({ todos })
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.