Using React's immutable helper with Immutable.js

丶灬走出姿态 提交于 2019-12-10 13:15:14

问题


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 is:

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

different from

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

回答1:


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'}`

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.



来源:https://stackoverflow.com/questions/27286134/using-reacts-immutable-helper-with-immutable-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!