我正在尝试使用immutable.js使代码整洁。
但是我不知道如何将更新替换为updateIn。请帮助我。
[SET_COLOR]: (state, action) => {
const counters = state.get("counters");
const { color, index } = action.payload;
return state.set(
"counters",
counters.updateIn([index, "color"], color => /*I got stuck here*/ )
);
}
我尝试过{color},({color}),color,color = color ...
这是原始的。
const initialState = Map({
counters: List([
Map({
number: 0,
color: "black"
})
])
});
(...)
[SET_COLOR]: (state, action) => {
const counters = state.get("counters");
const { color, index } = action.payload;
return state.set(
"counters",
counters.update(index, counter => counter.set("color", color))
);
},
答案 0 :(得分:0)
您应该改为尝试setIn:
[SET_COLOR]: (state, action) => {
const { color, index } = action.payload;
return state.setIn(["counters", index, "color"], color);
}
updateIn
仅在更新后的值取决于提供给updateIn
的路径上的当前值时才有用。例如:
const immutableObject = Immutable.fromJS({ outerProp: { innerCount: 1 } });
immutableObject.updateIn(['outerProp', 'innerCount'], count => count + 1);