4 ms·
I find dependency arrays to be neither intuitive or easy to debug. When you're missing a parameter or use the wrong one React can emit some pretty weird side ef
by debaserab2 5y ago
I find dependency arrays to be neither intuitive or easy to debug. When you're missing a parameter or use the wrong one React can emit some pretty weird side effects that are hard to track down back to the dependency array.
I generally like shaping my components as functions first and use hooks because I have to in order to do this but definitely not because I find them easier to understand than component lifecycle methods (even if they may be less verbose).
To me, hooks are the ugly part, writing component as a single function is the part that makes it worth it.
- jitl 5y agoDo you use the exhaustive-hooks ESLint rule? I think without it hooks are unusable in a team setting.
- brundolf 5y agoIt helps a lot, but it has gaps, because it reasons about syntax and not values. A complete solution is intractable without integrating this stuff at a language/type-system level (and I don't mean TypeScript; TypeScript can't represent hooks' relationships either).
- nightpool 5y agoThe general advice is to write your hooks without dependency arrays first, and then only add dependency arrays once you know what your hook body looks like. I find that that generally makes them much easier to debug. Also, yes, the exhaustive-hooks ESlint rule is a life-saver.