Immutablejs 和 shouldComponentUpdate
Immutablejs and shouldComponentUpdate
如果我正在使用 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
使用 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
returns false
.
如果我正在使用 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
使用 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
returns false
.