Last active
July 18, 2022 04:40
-
-
Save coryhouse/fdc5eae7dd25e5db68b684e6cb5c46a4 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
Being an immutable-related post you should probably use
const
instead oflet
:)