// Save search term state to React Hooks with spread operator and wrapper function
// Using .concat(), no wrapper function (not recommended)
setSearches(searches.concat(query))
// Using .concat(), wrapper function (recommended)
setSearches(searches => searches.concat(query))
// Spread operator, no wrapper function (not recommended)
setSearches([...searches, query])
// Spread operator, wrapper function (recommended)
setSearches(searches => [...searches, query])
this.setState(prevState => ({
myArray: [...prevState.myArray, "new value"]
}))
this.setState({ myArray: [...this.state.myArray, 'new value'] }) //simple value
this.setState({ myArray: [...this.state.myArray, ...[1,2,3] ] }) //another array
this.setState(prevState => ({
myArray: ["new value", ...prevState.myArray]
}))
this.setState(prevState => ({
myArray: [...prevState.myArray, {"name": "object"}]
}))
this.setState(prevState => ({
myArray: [ {"name": "object"}, ...prevState.myArray]
}))