-
-
Save obonyojimmy/4fbdb52a245ecdcde087ec8e8f9940c2 to your computer and use it in GitHub Desktop.
Handling React state via Immutable.js map
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
| // At top, import immutable | |
| import { Map } from 'immutable'; | |
| // Later, in constructor... | |
| this.state = { | |
| // Create an immutable map in state using immutable.js | |
| user: Map({ firstName: 'Cory', lastName: 'House'}) | |
| }; | |
| updateState({target}) { | |
| // this line returns a new user object assuming an immutable map is stored in state. | |
| let user = this.state.user.set(target.name, target.value); | |
| this.setState({user}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment