/immutable-state-helper

Immutable.js helper functions for React.js

Primary LanguageJavaScriptMIT LicenseMIT

immutable-state-helper

npm version CI report codecov.io immutable-state-helper dependency status immutable-state-helper devDependency status

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

  1. thisArg (*): The this binding of React Element.
  2. [field='data'] (String): The property name that stored in state.
  3. [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>;
  }
}