3 ms·
I'll disagree with this a bit. It was always possible to do this in class components if you had some kind of a `setState()` call in your `componentDidUpdate` m
by acemarke 6y ago
I'll disagree with this a bit.
It was always possible to do this in class components if you had some kind of a `setState()` call in your `componentDidUpdate` method. But, this was a rarer stumbling block.
On the other hand, with hooks:
- Effect hooks always run after render by default unless you specify a deps array to limit how often they re-run
- It's fairly easy to queue a state update that changes one of the values in your deps array, causing the effect hook to immediately run again
So, it's "fundamentally wrong" in that it's a thing you don't _want_ to do, but it _is_ a lot easier to make that mistake now.
- crooked-v 6y agoIf you're using setState in useEffect directly in a component then you're probably doing something wrong. All the valid use cases I can think of for functionality like that should be using a wrapping hook function, rather than re-implementing functionality separately in components.