Created
August 22, 2017 19:34
-
-
Save ambethia/1e79c46d5065ff27d162235717c50f05 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const item = 'FOOBerries!' | |
// Anti-pattern, don't do this! | |
this.state.list.push(item) | |
this.setState({ list: this.state.list }) | |
// Correct way to do what the anti-pattern did: | |
const nextState = this.state.list.slice() | |
nextState.push(item) | |
this.setState({ list: nextState }) | |
// ...but we can do better. | |
// Better! | |
this.setState({ | |
list: [item, ...this.state.list] | |
}) | |
// But sometimes we need to mutate more than just a simple array... | |
// Using immutability helper (https://github.com/kolodny/immutability-helper) | |
this.setState( | |
update(this.state, { | |
list: { $push: [item] } | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment