Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Forked from coryhouse/immutableJsExample.js
Created November 18, 2017 18:53
Show Gist options
  • Select an option

  • Save obonyojimmy/4fbdb52a245ecdcde087ec8e8f9940c2 to your computer and use it in GitHub Desktop.

Select an option

Save obonyojimmy/4fbdb52a245ecdcde087ec8e8f9940c2 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});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment