Skip to content

Instantly share code, notes, and snippets.

@coryhouse
Last active July 18, 2022 04:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coryhouse/fdc5eae7dd25e5db68b684e6cb5c46a4 to your computer and use it in GitHub Desktop.
Save coryhouse/fdc5eae7dd25e5db68b684e6cb5c46a4 to your computer and use it in GitHub Desktop.
Handling React state via Immutable.js map
// 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});
}
@tokland
Copy link

tokland commented May 19, 2018

Being an immutable-related post you should probably use const instead of let:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment