Reputation: 2833
I have a initialState:
let initialState = {
items: []
}
and i have a payload from my action like that eg:
{name: 'A', age: 2}, {name: 'B', age: 1}
after setting my payload to my reducer:
case SET_ITEMS:
return {
...state,
items: [...state.items, ...action.payload],
};
after that my state is:
let initialState = {
items: [
{name: 'A', age: 2},
{name: 'B', age: 1}
],
}
so I want to add to each item in items eg one more property like that:
let initialState = {
items: [
{name: 'A', age: 2, active: true},
{name: 'B', age: 1, active: true}
],
}
how i can do that?
Upvotes: 0
Views: 58
Reputation: 255
if you want to update both payload and state to add extra property you can also do this.
case SET_ITEMS:
return {
...state,
items: [
...state.items.map(item => ({
...item,
active: true
})),
...action.payload.map(item => ({
...item,
active: true
}))
],
};
Upvotes: 0
Reputation: 10873
You can use map
fro this:
case SET_ITEMS:
return {
...state,
items: state.items.map(item => ({
...item,
active: true
}))
};
Upvotes: 1