2017-05-14 60 views
0

在減速,我有:如何正確減速改變狀態,以便組件可以接收更新

if (action.type === 'remove_item') { 
     delete state.itemsList[action.itemId]; 
     return { 
     ...state 
     } 

但現在componentWillReceiveProps(nextProps)看不出區別:

this.props.items !== nextProps.items 

我知道,這是因爲我直接在reducer中更改狀態
,但不知道如何現在傳播此更改,以便nextProps將具有新的狀態數據?

回答

3

嘗試

...state.itemsList.filter((_, index) => index !== action.itemId) 

所以你不變異的原始狀態,而不是創建一個新的數組。

3

它沒有顯示更改,因爲狀態對象是不可變的,而且您直接在不可變對象上執行操作。

試試這個:

if (action.type === 'remove_item') { 
     var list = state.itemsList.splice(); 
     list[action.itemId] = null 
     return { 
     ...state, 
     itemsList: list, 
     } 

乾杯:)

相關問題