使用 List 和 return 整个状态(Redux)更改 ImmutableJS 嵌套对象
Changing ImmutableJS nested object with List and return whole state (Redux)
我的状态是嵌套的不可变事物:
const state = Map({ counter: 0,
people: List([
Map({name: "John", age: 12, etc...}),
Map({name: "Jim", age: 13, etc...}),
Map({name: "Jack", age: 21, etc...})
])
});
所以我有一个地图,其中包含一个计数器和地图列表。我在这里简化了一些事情,但是说我想在我的减速器中更改 John 的一些属性。
现在我正在做这样的事情:
var newState = state
.get('people') //get the list
.get(action.payload.pos) //pos is an integer, & describes the position of the object in the List
.set('name', action.payload.name)
.set('age', action.payload.age);
我的问题是我不知道如何在 John 中设置属性并取回整个状态 - 所以我可以 return 在我的减速器中.现在我得到的只是我正在改变的部分。
第二个问题是写下所有这些的方式很长。我知道有嵌套结构的语法,但我这里有一个 List
,它打破了这个,所以我有点卡住了。
您可以使用 .findIndex
找到要更新的索引(如果您还没有),然后使用 .updateIn
call, together with .merge
中的索引合并旧值和新值.
const state =
new Immutable.Map({
counter: 0,
people: new Immutable.List([
new Immutable.Map({name: "John", age: 12, otherVal: 'a'}),
new Immutable.Map({name: "Jim", age: 13, otherVal: 'b'}),
new Immutable.Map({name: "Jack", age: 21, otherVal: 'c'})
])
});
const newValues = { name: 'Jones', age: 100};
// find index to update
const indexToUpdate = state
.get('people')
.findIndex(person => person.get('name') == 'Jim');
// use .updateIn to perform a merge on the person of interest
const newState =
state.updateIn(
['people', indexToUpdate],
person => person.merge(newValues)
);
console.log(
'new state is:\n',
JSON.stringify(newState.toJS(), null, 2)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.2/immutable.min.js"></script>
我的状态是嵌套的不可变事物:
const state = Map({ counter: 0,
people: List([
Map({name: "John", age: 12, etc...}),
Map({name: "Jim", age: 13, etc...}),
Map({name: "Jack", age: 21, etc...})
])
});
所以我有一个地图,其中包含一个计数器和地图列表。我在这里简化了一些事情,但是说我想在我的减速器中更改 John 的一些属性。
现在我正在做这样的事情:
var newState = state
.get('people') //get the list
.get(action.payload.pos) //pos is an integer, & describes the position of the object in the List
.set('name', action.payload.name)
.set('age', action.payload.age);
我的问题是我不知道如何在 John 中设置属性并取回整个状态 - 所以我可以 return 在我的减速器中.现在我得到的只是我正在改变的部分。
第二个问题是写下所有这些的方式很长。我知道有嵌套结构的语法,但我这里有一个 List
,它打破了这个,所以我有点卡住了。
您可以使用 .findIndex
找到要更新的索引(如果您还没有),然后使用 .updateIn
call, together with .merge
中的索引合并旧值和新值.
const state =
new Immutable.Map({
counter: 0,
people: new Immutable.List([
new Immutable.Map({name: "John", age: 12, otherVal: 'a'}),
new Immutable.Map({name: "Jim", age: 13, otherVal: 'b'}),
new Immutable.Map({name: "Jack", age: 21, otherVal: 'c'})
])
});
const newValues = { name: 'Jones', age: 100};
// find index to update
const indexToUpdate = state
.get('people')
.findIndex(person => person.get('name') == 'Jim');
// use .updateIn to perform a merge on the person of interest
const newState =
state.updateIn(
['people', indexToUpdate],
person => person.merge(newValues)
);
console.log(
'new state is:\n',
JSON.stringify(newState.toJS(), null, 2)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.2/immutable.min.js"></script>