Answer the question
In order to leave comments, you need to log in
How to quickly change a lot of state values?
There is a component with set of the states connected among themselves. If the boolean value of one state changes to true, then all others must become false, for example. Is it possible to somehow select a set of states at a time and give them the desired boolean value, so as not to prescribe the states one by one and not assign the same value to each?
showWindow1= () => {
this.setState(() => {
return {
openWindow1: true,
openWindow2: false,
openWindow3: false,
openWindow4: false,
openWindow5: false
}
})
}
Answer the question
In order to leave comments, you need to log in
this.setState(state => ({
...Object.keys(state).reduce((acc, n) => (acc[n] = false, acc), {}),
свойствоКотороеДолжноБытьTrue: true,
}));
this.setState(state =>
Object.fromEntries(Object.keys(state).map(n => [ n, n === 'свойствоКотороеДолжноБытьTrue' ]))
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question