Javascript 为什么我的状态不使用immutableJS更改?

Javascript 为什么我的状态不使用immutableJS更改?,javascript,reactjs,redux,immutable.js,Javascript,Reactjs,Redux,Immutable.js,我正在尝试将immutableJS添加到Mern.io。当我试图从我的帖子列表中删除帖子,然后将其设置回我的状态时,状态不会更新 case ActionTypes.DELETE_POST : const removeArray = state.get('posts') .filter((post) => post._id !== action.post._id) console.log(removeArray.length) state.

我正在尝试将immutableJS添加到Mern.io。当我试图从我的帖子列表中删除帖子,然后将其设置回我的状态时,状态不会更新

case ActionTypes.DELETE_POST :
    const removeArray = state.get('posts')
              .filter((post) => post._id !== action.post._id)
    console.log(removeArray.length)
    state.set('posts', removeArray)
    console.log(state)
    return state;

在本例中,如果我有一个5的数组,我应该能够过滤掉它,然后用新数组再次设置“posts”。我不明白的是,我可以从数组中删除对象,并且removeArray将比state.posts少一个。但当我使用控制台日志状态时,它是相同的。我缺少什么?

当您调用
state.set(…)
时,它会返回一个新对象。原始的
状态
不变。我在您的代码段中更改了3行:

case ActionTypes.DELETE_POST :
    const removeArray = state.get('posts')
              .filter((post) => post._id !== action.post._id)
    console.log(removeArray.length)
    const newState = state.set('posts', removeArray) // assign to another variable
    console.log(newState) // and log here
    return newState; // and you'll probably want to return the new state

切换到
state.update('posts',…)也是有意义的。
@zerkms感谢您更新代码片段@ffffranky谢谢这真的把事情弄清楚了。在我的脑海中,每当你为一个不可变的对象的一部分设置一个新的值时,它就会保存一个历史记录。有人能投票支持我的问题吗?因为回答很有帮助,我觉得它很好地澄清了问题。