Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djD-REK/2eb6b333157b304c161f649855880726 to your computer and use it in GitHub Desktop.
Save djD-REK/2eb6b333157b304c161f649855880726 to your computer and use it in GitHub Desktop.
// 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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment