Created
October 16, 2019 06:35
-
-
Save Vincenius/2bc62da629782ae69428f3e8bf97d230 to your computer and use it in GitHub Desktop.
Toggle array item in react state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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