我是immuatblejs的新手,我很困惑,我的程序看起来不错时,我收到了无效的密钥路径错误
javascript - 一切正常时,不可变js中的KeyPath无效?-LMLPHP

我的状态

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


在componentMounted之后,主键将用数组填充。我只想替换或更新对象的嵌套数组值

最佳答案

您应该在master指令之前验证setIn属性的内容。如果属性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!' }引入不可变对象中。您确定那是您的意图吗?

09-10 12:18
查看更多