2017-05-14 31 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, 
     } 

干杯:)

相关问题