2016-10-22 24 views
0

我在我的反應應用程序中使用Immutable js!作爲優化的一部分,我嘗試在shouldComponentUpdate中使用shallowCompare,並且當我發現shallowCompare對於不變的狀態和道具返回true時!我在我的道具中有一個路徑和模塊鍵,它們是不可變的對象(分別是列表和地圖!)我不確定我是做錯了什麼或者比較淺的比較不支持不可變的js,你們可以幫我嗎?ShallowCompare不能與不可變的js

+0

也許有些代碼? –

回答

1

在大多數情況下,shallowCompare完美地適用於不可變對象。

如果您想要Immutable.is()的特殊支持,您可以使用shallowEqualImmutable。它更好地理解Immutable集合,因爲它認爲相同值的列表是相同的。

import React from 'react'; 
import { shallowEqualImmutable } from 'react-immutable-render-mixin'; 
class Test extends React.Component { 
    shouldComponentUpdate(nextProps, nextState) { 
    return !shallowEqualImmutable(this.props, nextProps) || 
      !shallowEqualImmutable(this.state, nextState); 
    } 

    render() { 
    return <div></div>; 
    } 
} 
+1

非常感謝,我非常感謝:D –