Hacker News new | ask | show | jobs
by jbaudanza 2381 days ago
Yes, I have had the same frustrations. The dependency array pattern feels very brittle. I have found two ways to deal with this:

1) Instead of taking a callback, can your hook return some value instead? I've found that in most cases, it can. That value can then be used to in some other (hopefully shorter) dependency array for a useEffect() hook that is a peer to your original hook.

2) In cases where your hook must take a callback, your hook should store the latest version of the callback in a local useRef. This add a lot more boilerplate code, but it feels safer to me than requiring the caller to manage the identity of the callback.

I've just had to make peace with the fact that I'm going to have to write a lot of boilerplate glue code around hooks. I try not to worry about it and I just hope the next version of hooks is better.

1 comments

Or you can just use classes? Where writing these dependencies is actually formalized?