Reactjs 如何正确更改reducer中的状态,以便组件可以接收更新
在这方面,我有:Reactjs 如何正确更改reducer中的状态,以便组件可以接收更新,reactjs,react-native,redux,react-redux,Reactjs,React Native,Redux,React Redux,在这方面,我有: if (action.type === 'remove_item') { delete state.itemsList[action.itemId]; return { ...state } 但是现在组件将接收道具(nextrops)看不出以下两者之间的区别: this.props.items !== nextProps.items 我知道发生这种情况是因为我直接在reducer中更改了状态,但现在不知道如何传播此更改,以
if (action.type === 'remove_item') {
delete state.itemsList[action.itemId];
return {
...state
}
但是现在组件将接收道具(nextrops)
看不出以下两者之间的区别:
this.props.items !== nextProps.items
我知道发生这种情况是因为我直接在reducer中更改了状态
,但现在不知道如何传播此更改,以便下一步操作将有新的状态数据?它没有显示更改,因为状态对象是不可变的,您直接在不可变对象上执行操作 试试这个:
if (action.type === 'remove_item') {
var list = state.itemsList.splice();
list[action.itemId] = null
return {
...state,
itemsList: list,
}
干杯:)它没有显示更改,因为状态对象是不可变的,您直接对不可变对象执行操作 试试这个:
if (action.type === 'remove_item') {
var list = state.itemsList.splice();
list[action.itemId] = null
return {
...state,
itemsList: list,
}
干杯:)试试看
...state.itemsList.filter((_, index) => index !== action.itemId)
因此,您没有改变原始状态,而是创建了一个新数组。试试看
...state.itemsList.filter((_, index) => index !== action.itemId)
因此,您没有改变原始状态,而是创建一个新数组