Answer the question
In order to leave comments, you need to log in
How to change nested properties in redux?
It is necessary to change the semi-qty of one of the array elements, this element is transferred to the payload action
//an array of objects
items = [
{
id: '1',
desc: 'Some description',
qty: 1
},
{
id: '2',
desc: 'Another description',
qty: 1,
},
и т.д.
export const incrementQty = (item) => {
return {
type: INCREMENT_QTY,
payload: item
}
};
const initialState = {
items: items
};
export default function(state = initialState, action) {
const {type, payload} = action;
switch (type) {
case INCREMENT_QTY:
const newItem = state.items.filter(item => item === payload);
return {
...state,
newItem: {
...newItem,
[newItem.qty]: 10
}
};
default:
return state
}
}
function updateVeryNestedField(state, action) {
return {
...state,
first : {
...state.first,
second : {
...state.first.second,
[action.someId] : {
...state.first.second[action.someId],
fourth : action.someValue
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
case INCREMENT_QTY:
const { payload: newItem } = action;
return {
...state,
items: state.items.map(item => {
if(item.id === newItem.id) {
return {
...item,
qty: newItem.qty,
}
}
return item;
})
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question