2 ms·
> There is also a proclivity to accidentally introduce infinite loops with hooks, which was a rare problem when dealing with classes. That makes me quirk an ey
by crooked-v 6y ago
> There is also a proclivity to accidentally introduce infinite loops with hooks, which was a rare problem when dealing with classes.
That makes me quirk an eyebrow. Somebody's doing something fundamentally wrong in how they're writing the component if they're making an infinite loop happen with hooks.
- acemarke 6y agoI'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.