Uncaught Error: invalid keyPath in immutable js?

Uncaught Error: invalid keyPath in immutable js?

我只是创建一个函数来使用不可变的 js 从我的状态更新现有值,即使代码对我来说看起来很好它抛出无效的 KeyPath 错误

        const initialState = fromJS({
            all:[],
            tags:[],
            isPaginationData:true,
            isContactFetchingPagination:false
        });
    .....
        const findAndUpdateContact = (state,action) => {
            let {payload} = action;
            let findIndex = state.get('all').findIndex(i => i._id === payload._id);
            if(findIndex !== -1){
                console.log(typeof findIndex,findIndex);// Returns number , 0

                console.log(state.deleteIn(['all',findIndex]),"New State"); // not deleting throws an error

                return state
            }else {
                return state
            }
        };

....

找到要更新的索引后,您可以使用 setIn 更新数据。 我会先找到索引(就像你做的那样):

let findIndex = state.get('all').findIndex(i => i.get('_id') === payload._id);

然后更新状态:

return state.setIn(['all', findIndex], 'new state');