setArray(oldArray => [...oldArray,newValue] );
// 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 => ({
arrayvar: [...prevState.arrayvar, newelement]
}))
stackoverflow:https://stackoverflow.com/questions/37435334/correct-way-to-push-into-state-array