当一切看起来都很好时,不可变 js 中的无效 KeyPath?

invalid KeyPath in immutable js when everything looks fine?

我是 immuatblejs 的新手,我很困惑,当我的程序看起来不错时,我遇到了无效的密钥路径错误

我的州

const initialState = fromJS({
    text:null,
    master:null,
    inputBoxStatus:false
});

master key会在componentMounted后填充数组我只想替换或更新objects的嵌套数组值

您应该在 setIn 指令之前验证 master 属性 的内容。如果 属性 master 包含 immutable List,它应该可以正常工作,如本例所示:

const { fromJS } = require('immutable');

const initialState = fromJS({
    text: null,
    master: [{ something: 'fizz' }, { other: 'buzz' }],
    inputBoxStatus: false
});

const modifiedState = initialState.setIn(['master', 0], { test: 'works!' });

console.log(modifiedState.get('master')); // List [ [object Object], Map { "other": "buzz" } ]

但是,如果它包含 null,它会失败并显示您显示的错误消息:

const initialState = fromJS({
    text: null,
    master: null,
    inputBoxStatus: false
});

const modifiedState = initialState.setIn(['master', 0], { test: 'works!' });

另请注意,您在不可变对象中引入了可变对象 { test: 'works!' }。你确定那是你想要的吗?