4 ms·
I really like React, but I have to admit, that I don't write big apps with it (yet). There is one thing though which I find results in subtil bugs (entirely my
by rmetzler 11y ago
I really like React, but I have to admit, that I don't write big apps with it (yet).
There is one thing though which I find results in subtil bugs (entirely my fault): When one input is changing and depending on that input another output value has to change than the input shouldn't change state yet and the output should be computed from input (+ state where necessary). Only then you should set the state.
Do not set the state before computing the output and don't compute the output from the previous state only. This will result in subtle and sometimes hard-to-find bugs.
- danabramov 11y agoIf you’re saying that `this.state` doesn’t update immediately after `this.setState` so you can‘t use the previous value, one way to get around this is to use `this.setState` overload that takes a function: this.setState(prevState => ({ prevState.count + 1 })) This way all the changes will be stacked on top of each other correctly.