Immutablejs和shouldComponentUpdate

时间:2016-07-09 16:49:04

标签: reactjs redux react-redux immutable.js

如果我正在使用Immutable.js,react-redux仍然可以使用shouldComponentUpdate吗? connect()方法在shouldComponentUpdate()中使用shallowEqual但是从Immutable.js文档我看到我们必须使用Immutable自己的equals()方法来检查相等而不是===运算符(shallowEqual使用)

考虑一下:

const map1 = Immutable.Map({a:1, b:2, c:3});
const map2 = Immutable.Map({a:1, b:2, c:3});

map1 === map2 // returns false
map1.equals(map2) // returns true

1 个答案:

答案 0 :(得分:4)

使用GET的重点是在底层对象实际上没有改变时保留引用。 immutable.js在属性之间执行快速相等检查,这比使用shallowEqual深度比较值有很大的收获。

示例:

immutable.equals

在您的示例中,您明确分配了两个不同的let state = Immutable.Map({a: 1, b:2, c:3)} let state2 = state.set('a', 1) state === state2 //true because Immutable returns the same reference object since there is no changes 对象,因此它是内存中的两个不同对象,Immutable.Map返回map1 === map2

相关问题