Skip to content

Instantly share code, notes, and snippets.

@Vincenius
Created October 16, 2019 06:35
Show Gist options
  • Save Vincenius/2bc62da629782ae69428f3e8bf97d230 to your computer and use it in GitHub Desktop.
Save Vincenius/2bc62da629782ae69428f3e8bf97d230 to your computer and use it in GitHub Desktop.
Toggle array item in react state
onToggleArray = item => {
this.setState(state => {
const arr = state.arr.includes(item)
? arr.filter(i => i !== item) // remove item
: [ ...arr, item ]; // add item
return {
arr,
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment