Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当一切正常时,不可变js中的密钥路径无效?_Javascript_Reactjs_Immutable.js - Fatal编程技术网

Javascript 当一切正常时,不可变js中的密钥路径无效?

Javascript 当一切正常时,不可变js中的密钥路径无效?,javascript,reactjs,immutable.js,Javascript,Reactjs,Immutable.js,我是immuatblejs新手,我很困惑,当我的过程看起来很好的时候,我得到了无效的密钥路径错误 我的状态 const initialState = fromJS({ text:null, master:null, inputBoxStatus:false }); 主密钥在组件安装后将填充数组我只想替换或更新对象的嵌套数组值您应该在setIn指令之前验证master属性的内容。如果属性master包含一个不可变列表,它应该可以正常工作,如下例所示: const { f

我是immuatblejs新手,我很困惑,当我的过程看起来很好的时候,我得到了无效的密钥路径错误

我的状态

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

主密钥在组件安装后将填充数组我只想替换或更新对象的嵌套数组值

您应该在
setIn
指令之前验证
master
属性的内容。如果属性
master
包含一个
不可变列表
,它应该可以正常工作,如下例所示:

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!'}
。您确定这就是您想要的吗?

您应该在
setIn
指令之前验证
master
属性的内容。如果属性
master
包含一个
不可变列表
,它应该可以正常工作,如下例所示:

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!'}
。你确定这就是你想要的吗?

test:works
只是一个例子,我有不同的值,关于主状态,一旦安装组件,它将填充数组值,当onChange函数触发时,这个setIn选项将触发,所以不会有问题,对吗?正如我所说,如果不知道
master
属性的内容,就很难判断。我建议您执行
console.log(yourState.get('master'))
就在执行
设置in
指令的行之前。这样,您可以在控制台中看到
主属性的内容。
测试:工作
仅举一个例子,我有不同的值,关于主状态,一旦安装组件,它将填充数组值,并且一旦更改,此设置in选项将触发ge函数触发,所以不会有问题,对吗?正如我所说,如果不知道
master
属性的内容,很难判断。我建议您执行
console.log(yourState.get('master'))
就在执行
设置指令的行之前。这样,您可以在控制台中看到
主属性的内容。