When to use .toJS() with Immutable.js and Flux?

后端 未结 5 624
礼貌的吻别
礼貌的吻别 2021-01-30 16:38

I\'m trying to use ImmutableJS with my React / Flux application.

My stores are Immutable.Map objects.

I\'m wondering at which point should I use

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 17:27

    Ideally, never!

    If your Flux stores are using Immutable.js then try to maintain all the way through. Use React.addons.ReactComponentWithPureRenderMixin to achieve a memoization performance win (it adds a shouldComponentUpdate methods).

    When rendering, you may need to call toJS() as React v0.12.x only accepts Array as children:

    render: function () {
      return (
        
    {this.props.myImmutable.map(function (item) {
    {item.title}
    }).toJS()}
    ); }

    This have changed in React v0.13.x. Components accept any Iterable as children instead of only Array. Since Immutable.js implements Iterable, you are able to omit the toJS():

    render: function () {
      return (
        
    {this.props.myImmutable.map(function (item) {
    {item.title}
    })}
    ); }

提交回复
热议问题