将React的不可变助手与Immutable.js一起使用 [英] Using React's immutable helper with Immutable.js

查看:128
本文介绍了将React的不可变助手与Immutable.js一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究助焊剂应用程序,我正在考虑采用 immutable.js 来维持状态。我看到react提供了自己的帮助器来更新不可变对象( http://facebook.github.io/react/docs /update.html ),但无法分辨它与不可变的自己的setIn和updateIn方法有多大不同(即,如果用setIn改变,我可以将对象与===进行比较)。是否有理由将react helper与immutable.js一起使用?它只是语法糖吗?

I'm working on a flux application and am considering adopting immutable.js to maintain state. I saw that react supplies its own helper for updating immutable objects (http://facebook.github.io/react/docs/update.html), but couldn't tell how it was much different from immutable's own setIn and updateIn methods (i.e, i can already compare objects with === to se if they changes with setIn). Is there a reason to use the react helper with immutable.js? Is it just syntactic sugar?

TL; DR是:

var map = Immutable.fromJS({bar: 'baz'});
map2 = React.addons.update(map, {
        bar: {$set: 'foo'}
    });

不同于

var map = Immutable.fromJS({bar: 'baz'});
map2 = map.set('bar', 'foo');


推荐答案

React.addons.update 不适用于Immutable.js值;它使用纯JavaScript对象和数组

React.addons.update does not work with Immutable.js values; it works with plain JavaScript objects and arrays.

var map = { bar: 'baz' };
var map2 = React.addons.update(map, {
  bar: {$set: 'foo'}
});
console.log(map2); // A plain JS object of value `{bar: 'foo'}`

不可变的类型的普通JS对象.js使用特殊数据实现结构,用于提高性能和空间效益;显然,React.addons.update消耗和生成的普通JavaScript对象不是。

The types in Immutable.js are implemented using special data structures for performance and space benefits; the plain JavaScript objects consumed and produced by React.addons.update, obviously, are not.

这篇关于将React的不可变助手与Immutable.js一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆