News for hackers. Stuff that matters.
Re:Understanding UseMemo and UseCallback by HeyImAlex on Tuesday August 30, 2022 @09:23PM (#32657264)
I don’t think it was done for performance, hooks just compose much better than class components. Definitely a trade off in terms of blank-slate intuition, but it unlocked a lot of things in the realm of reusability.
Re:Understanding UseMemo and UseCallback by foomatic on Wednesday August 31, 2022 @12:33AM (#32658409)
What does "compose much better" mean?
Re:Understanding UseMemo and UseCallback by HeyImAlex on Wednesday August 31, 2022 @01:16AM (#32658640)
copy/paste from another comment:

Hooks compose, whereas side effects and memoized values sprinkled through component constructors and lifecycle methods do not.

For example, the equivalent of useEffect required calls inside of componentWillMount, componentDidUpdate, and componentWillUnmount. You try and make something like this re-usable and you’ll be leaking details of your implementation across the whole component via inclusion in these lifecycle methods, not to mention any data you’re shoving onto the component instance. But it’s still doable.

Now, what if you wanted to use this re-usable behavior inside of another re-usable behavior? It gets complicated fast! Now your library needs to expose the lifecycle-updating methods of the underlying library, leaking details all the way down. Hooks are opaque from the perspective of lifecycle, while still having access to all the same… hooks.

Re:Understanding UseMemo and UseCallback by foomatic on Wednesday August 31, 2022 @01:51AM (#32658841)
Oh ok. I'd agree Hooks compose much better then. But there was no attempt or consideration at compositional API for class based components besides getting rid of it entirely and then adding functions that hook into the nether nether.

> For example, the equivalent of useEffect required calls inside of componentWillMount, componentDidUpdate, and componentWillUnmount.

Yeah from what I recall this was the prime motivational example given in the React docs for introducing hooks. It's a legit problem. But hooks and getting rid of class based components is not the only solution. In classical OO are also well worn solutions along the lines of hooks, they just work with classes / objects with interfaces still. It's somewhat frustrating that this is never even considered.

Re:Understanding UseMemo and UseCallback by captainmuon on Wednesday August 31, 2022 @02:51AM (#32659190)
It depends, they compose better until they don't. It is easy to mess hooks up if you do not call them in the right order, especially if they are nested (composed) in other functions. You need discipline to use them correctly.

Ironically they could have prevented this by making them more functional. Instead of some magic hidden state, you should have to always pass a context to them, and you should have to give every hook a unique name or a Symbol().

Re:Understanding UseMemo and UseCallback by HeyImAlex on Wednesday August 31, 2022 @10:18AM (#32662349)
I know they considered “keyed” hooks, but that api is a lot more verbose since you need to key _every_ hook you use, and hooks internally need to create a sub-hook context / namespace to avoid collisions which either needs to be managed manually _or_ means you can’t use plain functions as hooks (there would be some createHook function you’d run instead).