Answers for "react update state one value"

1

changing state value react

const [a, b] = React.useState(['hi','world']);
  const dup = [...a]; //won't work without spread operator
  b(dup);
Posted by: Guest on August-23-2021
0

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

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});
},
Posted by: Guest on July-17-2021

Code answers related to "react update state one value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language