2016-07-09 33 views
2

如果我使用Immutable.js,react-redux仍然可以使用shouldComponentUpdate嗎? connect()方法在shouldComponentUpdate()中使用shallowEqual,但是從Immutable.js文檔中我看到我們必須使用Immutable自己的equals()方法來檢查等於而不是===運算符(使用哪個shallowEqual)Immutablejs和shouldComponentUpdate

試想一下:

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 

回答

3

使用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回報false