npm install immutable-state-helper
Helper function for setState pattern.
Copy and modify from Immutable-as-React-state.
createImmState(thisArg, [field], [callback]): Function
- thisArg (*): The this binding of React Element.
- [field='data'] (String): The property name that stored in state.
- [callback] (Function): The callback apply into setState's callback.
(Function): Immutable version of setState.
import React from 'react';
import { Map } from 'immutable;
import { createImmState } from 'immutable-state-helper';
class Example extends React.Component {
...
onFooChange(value) {
this.setImmState(
updater => updater.set('foo', value),
() => console.log('re-rendered!')
);
}
constructor(props) {
super(props);
this.state = {
immData: Map({
foo: '',
bar: ''
})
}
this.setImmState = createImmState(this, 'immData');
}
render() {
const { immData } = this.state;
return <div>{ immData.get('foo', '') }</div>;
}
}