[英]Immutablejs - map not making nested objects immutable
I am setting up a map like so: 我正在像这样设置地图:
import { Map } from 'immutable';
const initialState = {
a: "test",
b: {
inside: "inside value"
}
}
const otherState = {
c: "other value"
}
const myState = new Map({ app: new Map(initialState).merge(otherState) });
This seemed to be working, however when I try to change values inside the nested object it does not seem to be working for me (I'm getting "invalid keypath"). 这似乎在起作用,但是当我尝试更改嵌套对象内的值时,它似乎对我不起作用(我得到了“无效的键路径”)。 So trying: 所以尝试:
myState.setIn(['app', 'b', 'inside'], 'newValue');
is giving me an "invalid keypath" error. 给我一个“无效的密钥路径”错误。 It's looking like When I log it out that the nested object is not being turned immutable. 看起来当我注销时,嵌套对象没有变成不可变的。 Unsure what I am doing incorrectly. 不确定我在做什么错。
Edit - here is a codepen to show the problem - https://codepen.io/ajmajma/pen/rRQoZp 编辑-这是一个显示问题的Codepen- https ://codepen.io/ajmajma/pen/rRQoZp
Apparently the correct path should be ['app', 'b', 'inside']
given that app
is the only property of the myState
object. 鉴于app
是myState
对象的唯一属性,因此显然正确的路径应为['app', 'b', 'inside']
。
You also need to Immutable.fromJS()
your initialState
and otherState
objects, otherwise nested keys are treated as JS objects, not immutablejs maps. 您还需要Immutable.fromJS()
您的initialState
和otherState
对象,否则嵌套键将被视为JS对象,而不是immutablejs映射。
const myState = new Map({ app: new Map(Immutable.fromJS(initialState)).merge(Immutable.fromJS(otherState)) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.