Hacker News new | ask | show | jobs
by codeflo 1551 days ago
I’ll prefix by saying I agree with a lot of the criticisms, and I’ve experienced first hand how hard to explain and error prone hooks are. But for this post, let me defend the concept.

The idea, in a very rough nutshell, is to allow separating behavior and presentation.

Hooks are the reusable unit of behavior. You compose hooks into more complex hooks that might implement loading and saving data from/to the server, for example.

Then you can use this hook with different components, or use the same component in a different context with a different data source. This can be very powerful if used well.

But as I said at the beginning, hooks are unfortunately also very difficult to get right.

1 comments

To expand on this, many of the things that hooks now make easy were bespoke per-class implementations with details that leaked into every lifecycle hook. Think of how you’d write a chain of useMemo calls in a class component to see just how bad it was.
Why would you need useMemo in a class component? You can just attach a memoized instance of the function to the component