Live data from Hacker News

Frustrations with React Hooks

blog.logrocket.com

41–50 of 139 posts

Re: Frustrations with React Hooks

#41
post #11

I absolutely love hooks and don't want to use the `React.Component` class ever again. And yes, hooks need to be grasped, they're quite something else. But once you get the hang of hooks, they're very simple to understand. OP seems to be a bit stuck in a hole. If you end up in a situation where your hooks come loose and the code becomes a mess — just delete them all and rethink your code / component logic structure. B…

We're slowly refactoring our class-heavy React app to functional components with hooks. One thing we haven't been able to replace, though, are classes that depend on refs. How to do this, since there's no instance with functions like there are with classes?

That's what the very-rarely-used `useImperativeHandle` hook is for - exposing "instance methods" from function components:

https://reactjs.org/docs/hooks-reference.html#useimperativeh...

Re: Frustrations with React Hooks

#42
post #11

I absolutely love hooks and don't want to use the `React.Component` class ever again. And yes, hooks need to be grasped, they're quite something else. But once you get the hang of hooks, they're very simple to understand. OP seems to be a bit stuck in a hole. If you end up in a situation where your hooks come loose and the code becomes a mess — just delete them all and rethink your code / component logic structure. B…

I don't understand the hate against class based components. React class-based components are dead simple to understand.

Re: Frustrations with React Hooks

#43
post #42
post #11

I absolutely love hooks and don't want to use the `React.Component` class ever again. And yes, hooks need to be grasped, they're quite something else. But once you get the hang of hooks, they're very simple to understand. OP seems to be a bit stuck in a hole. If you end up in a situation where your hooks come loose and the code becomes a mess — just delete them all and rethink your code / component logic structure. B…

I don't understand the hate against class based components. React class-based components are dead simple to understand.

Nothing is simpler than a function. Easier to write, debug, and test.

* Removed "pure" to make my main point clear.

Re: Frustrations with React Hooks

#44
post #11

I absolutely love hooks and don't want to use the `React.Component` class ever again. And yes, hooks need to be grasped, they're quite something else. But once you get the hang of hooks, they're very simple to understand. OP seems to be a bit stuck in a hole. If you end up in a situation where your hooks come loose and the code becomes a mess — just delete them all and rethink your code / component logic structure. B…

That seems a bit unfair. There are some weird edge cases with useEffect, no question. I don’t think they’re bad enough to want to go back to using classes... but I think the OPs post was a bit more specific than you’re giving them credit for, and they stuff like useAsyncEffect exists because it is a common pain point. For example, this useRef / useEffect combo mystifies me even now: https://stackblitz.com/edit/react-…

I suspect this may be one of those cases where there's an 'aha' moment involved, and people who get that moment early find it simple to understand, people who haven't got to that moment yet are going argh, and people who got to it late sigh and/or write "yet another X tutorial" in the hopes that how they got their aha moment will work for other people.

The classic X here, of course, is monads.

But as personal examples, it took me ages of headbanging to get the 'aha' moment for git, even though a bunch of people I know got it almost immediately.

In the case of hooks, once I built a mental model of how they were (theoretically) implemented, I went 'ooh' and found them obvious and relatively easy from then on. It looks to me like the author of this piece didn't have that luck, and so for him, they're not simple (although maybe later, post-aha-moment, they will be).

tl;dr: "They seem simple to understand once you understand them, but are mystifying up until that point" seems to me to be the most likely hypothesis here.

Re: Frustrations with React Hooks

#45
post #42

Earlier quoted context omitted.

I don't understand the hate against class based components. React class-based components are dead simple to understand.

Nothing is simpler than a function. Easier to write, debug, and test. * Removed "pure" to make my main point clear.

But functions that use hooks aren't pure because hooks break referential transparency.

Re: Frustrations with React Hooks

#46
post #42

Earlier quoted context omitted.

I don't understand the hate against class based components. React class-based components are dead simple to understand.

Nothing is simpler than a function. Easier to write, debug, and test. * Removed "pure" to make my main point clear.

How are your functions pure if inside them you call `useEffect`, `useState` ? Where is pureness when at first look they generate side effects, or am I missing something ?

Re: Frustrations with React Hooks

#47
post #46

Earlier quoted context omitted.

Nothing is simpler than a function. Easier to write, debug, and test. * Removed "pure" to make my main point clear.

How are your functions pure if inside them you call `useEffect`, `useState` ? Where is pureness when at first look they generate side effects, or am I missing something ?

Not all functions needs hooks, not all hooks break pureness.

But my point was functions are simpler in all those scenarios, pure or not. Edited my comment to reflect that.

Re: Frustrations with React Hooks

#49
post #2

interested in hearing more people experiences with React Hooks.

I've started to think in hooks which is good. Not that it is always clear, but seen as a way to connect function and presentation, hooks work well. At times I question the directionality of hooks - do they hook function to presentation, presentation to function or both? The useEffect hook appears to be a case of presentation => function whereas many others are the opposite, and that can make it more problematic, as others have pointed out.

I'm not sure this a valid understanding, but it works for me as a heuristic.

Post reply on HN