何时将.toJS()与Immutable.js和Flux结合使用? [英] When to use .toJS() with Immutable.js and Flux?
问题描述
我正在尝试在我的React/Flux应用程序中使用ImmutableJS.
I'm trying to use ImmutableJS with my React / Flux application.
我的商店是Immutable.Map
个对象.
我想知道在什么时候应该使用.toJS()
?商店的.get(id)
返回时应该是吗?或包含.get('member')
?
I'm wondering at which point should I use .toJS()
? Should it be when the store's .get(id)
returns ? or in the components with .get('member')
?
推荐答案
理想情况下,永远不会!
Ideally, never!
如果您的Flux商店使用的是Immutable.js,请尝试一直维护.使用React.addons.ReactComponentWithPureRenderMixin来获得备忘录性能(它添加了shouldComponentUpdate方法).
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).
渲染时,您可能需要调用toJS()
,因为React v0.12.x仅接受Array
作为子代:
When rendering, you may need to call toJS()
as React v0.12.x only accepts Array
as children:
render: function () {
return (
<div>
{this.props.myImmutable.map(function (item) {
<div>{item.title}</div>
}).toJS()}
</div>
);
}
这已在React v0.13.x中更改.组件接受任何Iterable作为子代,而不是仅接受Array
.由于Immutable.js实现了Iterable,因此您可以省略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 (
<div>
{this.props.myImmutable.map(function (item) {
<div>{item.title}</div>
})}
</div>
);
}
这篇关于何时将.toJS()与Immutable.js和Flux结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!