immutable-state-helper
v1.1.0
Published
Immutable.js helper functions for React.js
Downloads
4
Readme
immutable-state-helper
Install
npm install immutable-state-helper
createImmState
Helper function for setState pattern.
Copy and modify from Immutable-as-React-state.
Signature
createImmState(thisArg, [field], [callback]): Function
Arguments
- 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.
Returns
(Function): Immutable version of setState.
Example
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>;
}
}