Answer the question
In order to leave comments, you need to log in
How to correctly and beautifully update states for nested objects in React?
Here, for example, there is such a state:
this.state = {
parent : {
someChild: {
oneMoreChild: { name: 'Test', another: 'Test2' }
}
}
}
constructor(props) {
super(props)
const toState = {
parent : {
someChild: {
oneMoreChild: { name: 'Test', another: 'Test2' }
}
}
}
const { parent } = toState;
const {someChild} = parent;
const {oneMoreChild} = someChild;
const {name, another} = oneMoreChild;
this.state = {
toState,
name: name,
another: another,
}
}
Answer the question
In order to leave comments, you need to log in
immutability-helper (no matter how cumbersome it may seem, it solves its problem)
Ramda (lensProp/lensPath + set/over)
immer
1. map is a method for transforming data, not for iterating.
2. If only two properties are changeable, then it is not clear why you need to have a complex nested structure at all and keep it in state.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question