React: how to update state.item[1] in state using setState? React: how to update state.item[1] in state using setState? reactjs reactjs

React: how to update state.item[1] in state using setState?


Since there's a lot of misinformation in this thread, here's how you can do it without helper libs:

handleChange: function (e) {    // 1. Make a shallow copy of the items    let items = [...this.state.items];    // 2. Make a shallow copy of the item you want to mutate    let item = {...items[1]};    // 3. Replace the property you're intested in    item.name = 'newName';    // 4. Put it back into our array. N.B. we *are* mutating the array here, but that's why we made a copy first    items[1] = item;    // 5. Set the state to our new copy    this.setState({items});},

You can combine steps 2 and 3 if you want:

let item = {    ...items[1],    name: 'newName'}

Or you can do the whole thing in one line:

this.setState(({items}) => ({    items: [        ...items.slice(0,1),        {            ...items[1],            name: 'newName',        },        ...items.slice(2)    ]}));

Note: I made items an array. OP used an object. However, the concepts are the same.


You can see what's going on in your terminal/console:

❯ node> items = [{name:'foo'},{name:'bar'},{name:'baz'}][ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ]> clone = [...items][ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ]> item1 = {...clone[1]}{ name: 'bar' }> item1.name = 'bacon''bacon'> clone[1] = item1{ name: 'bacon' }> clone[ { name: 'foo' }, { name: 'bacon' }, { name: 'baz' } ]> items[ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ] // good! we didn't mutate `items`> items === clonefalse // these are different objects> items[0] === clone[0]true // we don't need to clone items 0 and 2 because we're not mutating them (efficiency gains!)> items[1] === clone[1]false // this guy we copied


You could use the update immutability helper for this:

this.setState({  items: update(this.state.items, {1: {name: {$set: 'updated field name'}}})})

Or if you don't care about being able to detect changes to this item in a shouldComponentUpdate() lifecycle method using ===, you could edit the state directly and force the component to re-render - this is effectively the same as @limelights' answer, as it's pulling an object out of state and editing it.

this.state.items[1].name = 'updated field name'this.forceUpdate()

Post-edit addition:

Check out the Simple Component Communication lesson from react-training for an example of how to pass a callback function from a state-holding parent to a child component which needs to trigger a state change.


Wrong way!

handleChange = (e) => {    const { items } = this.state;    items[1].name = e.target.value;    // update state    this.setState({        items,    });};

As pointed out by a lot of better developers in the comments: mutating the state is wrong!

Took me a while to figure this out. Above works but it takes away the power of React. For example componentDidUpdate will not see this as an update because it's modified directly.

So the right way would be:

handleChange = (e) => {    this.setState(prevState => ({        items: {            ...prevState.items,            [prevState.items[1].name]: e.target.value,        },    }));};