Steven Harlow
Steven Harlow

Reputation: 651

How to re-render react component when mapping over state that is array of objects

I am trying to map over an array of objects in state, conditionally returning one of two react components from that state. I then change that state at some point and would expect the component to re-render when it's object's state changed. I understand my issue is something to do with React not recognizing the change in the diff, but I'm not sure why and what pattern I need to change to in order to get this working.

Here's a codepen: https://codepen.io/steven-harlow/pen/KKPLXRO

And the code from it:

const App = (props) => {
  const [todos, setTodos] = React.useState([
    {name: 'A', done: false},
    {name: 'B', done: false},
    {name: 'C', done: false},
  ])

  React.useEffect(() => {

  }, [todos])

  const handleClick = (name) => {
    const index = todos.find(todo => todo.name == name)
    let tempTodos = todos;
    tempTodos[index].done = true;
    setTodos(tempTodos);
  }

  return (
    <div>
      <h1>Hello, world!</h1>
      <div>
        {todos.map(todo => {
          return todo.done ? (<div key={'done' + todo.name}>{todo.name} : done</div>) : (<div onClick={() => handleClick(todo.name)} key={'notdone' + todo.name}>{todo.name} : not done</div>)
        })}
      </div>
    </div>
  )
}

Upvotes: 3

Views: 11067

Answers (2)

Reactgular
Reactgular

Reputation: 54791

React won't see the state as changed unless you create a new array.

const handleClick = n => setTodos(todos.map(t => t.name === n ? {...t, done: true} : t));

Upvotes: 0

Justin Savala
Justin Savala

Reputation: 111

Here you go, this here should work for you now. I added some notes in there.

const App = (props) => {
  const [todos, setTodos] = React.useState([
    {name: 'A', done: false},
    {name: 'B', done: false},
    {name: 'C', done: false},
  ])

  const handleClick = (name) => {
    /*
      Here you were using todos.find which was returning the object. I switched
      over to todos.findIndex to give you the index in the todos array. 
    */
    const index = todos.findIndex(todo => todo.name === name)
    /*
      In your code you are just setting tempTodos equal to todos. This isn't
      making a copy of the original array but rather a reference. In order to create 
      a copy I am adding the .slice() at the end. This will create a copy.
      This one used to get me all of the time.
    */
    let tempTodos = todos.slice();
    tempTodos[index].done = true;
    setTodos(tempTodos);
  }
  console.log(todos)
  return (
    <div>
      <h1>Hello, world!</h1>
      <div>
        {todos.map((todo,index) => {
        return todo.done ? (<div key={index}>{todo.name} : done</div>) : (<div onClick={() => handleClick(todo.name)} key={index}>{todo.name} : not done</div>)
        })}
      </div>
    </div>
  )
}


ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Another thing I did was simplify the keys for the divs created by the map. I just added the index to the map and used that for the key, a lot cleaner that way.

Hope this helps!

Upvotes: 6

Related Questions