1this.setState(prevState => ({
2 myArray: [...prevState.myArray, "new value"]
3}))
4
1this.setState(prevState => ({
2 myArray: ["new value", ...prevState.myArray]
3}))
4
1this.setState({ myArray: [...this.state.myArray, 'new value'] }) //simple value
2this.setState({ myArray: [...this.state.myArray, ...[1,2,3] ] }) //another array
3
1this.setState(prevState => ({
2 myArray: [ {"name": "object"}, ...prevState.myArray]
3}))
4
1// Using hooks
2
3const [array, setArray] = useState([1, 2, 3])
4
5setArray(prevArray => [...prevArray, 4]); // -> [1, 2, 3, 4]