Answer the question
In order to leave comments, you need to log in
What is the best way to change the state into a reducer if the structure is a composite object?
I have this structure
const initialState = {
_id: '',
structure: {
0: {
id: 0,
_key: false,
_value: 1,
type: 'object',
childIds: []
}
},
name: '',
createdAt: '',
updatedAt: ''
};
const updateChildLevelNode = structure => {
const { id, value, key } = action;
structure[id]._value = value;
structure[id]._key = key;
return structure;
};
return {
...state,
structure: updateChildLevelNode(state.structure)
};
Answer the question
In order to leave comments, you need to log in
First: you are updating the structure incorrectly - in no case should you mutate an object.
Secondly: why not immediately form an action object with the necessary property names? (_key, _value)
const action = {
id: 132,
_key: 'some key',
_value: 564
}
return {
...state,
structure: {
...state.structure,
[id]: { ...(state.structure[id] || {}), ...action },
}
}
import _ from 'lodash'
const { id, parentId } = action
// удаляем из структуры свойство с именем id
const nextStructure = _.omit(state.structure, id)
return {
...state,
structure: {
...nextStructure,
[parentId]: {
...nextStructure[parentId],
childIds: nextStructure[parentId].childIds.filter(cId => cId !== id)
}
}
}
const { id, parentId } = action
// в данном случае у нас будет переменная
// const removed = state.structure[id]
// а в переменную nextStructure попадут все значения, кроме id
const { [id]: removed, ...nextStructure } = state.structure
return {
...state,
structure: {
...nextStructure,
[parentId]: {
...nextStructure[parentId],
childIds: nextStructure[parentId].childIds.filter(cId => cId !== id)
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question