Hacker News new | ask | show | jobs
by bocytron 1551 days ago
Can anyone here recommend a blog post that go through the most common mistakes when using hooks?
2 comments

Don't have a blog post, but the majority of issues you will find are with dependencies arrays for useMemo/useCallback/useEffect.

Most common I see:

1: Missing dependencies (for folks who don't use the linting rule)

2: Not understanding reacts async state model: example being expecting state to have been updated immediately after calling setState inside an effect

3: Not understanding closures: an example is creating an interval in an effect that uses and updates a useState value, but being confused why it isn't updated when the interval repeatedly fires.

I agree wholeheartedly with the author. Hooks are powerful and even I was super excited to start using them when they released. But now I think the hooks paradigm leads to even worse and bug-ridden code than what we had before.

The docs are pretty good, but don't seem to cover very well that when using hooks you really need to know JS well, which includes equality in JS, closures, etc otherwise you will be guaranteed to shoot yourself in the foot.

There is so much more business logic mixed in with component code at seemingly random in projects as well such that it makes finding where "stuff" happens even more difficult than before.

The article here ("Hooks Considered Harmful") actually probably would be better called "Most Common Mistakes When Using Hooks". It seems to cover the most common ones I'm aware of.