Answers for "how to add array data on state react"

2

add new array at the back of react state

this.setState(prevState => ({
  myArray: [...prevState.myArray, "new value"]
}))
Posted by: Guest on December-22-2020
5

how to add array data on state react

this.setState({ myArray: [...this.state.myArray, 'new value'] }) //simple value
this.setState({ myArray: [...this.state.myArray, ...[1,2,3] ] }) //another array
Posted by: Guest on November-11-2020
1

add new array at the back of react state

this.setState(prevState => ({
  myArray: ["new value", ...prevState.myArray]
}))
Posted by: Guest on December-22-2020
0

add new array at the back of react state

this.setState(prevState => ({
  myArray: [ {"name": "object"}, ...prevState.myArray]
}))
Posted by: Guest on December-22-2020
0

how to add array data on state react

// Using hooks

const [array, setArray] = useState([1, 2, 3])

setArray(prevArray => [...prevArray, 4]); // -> [1, 2, 3, 4]
Posted by: Guest on April-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language