Live data from Hacker News

A Critique of React Hooks

dillonshook.com

131–140 of 298 posts

Re: A Critique of React Hooks

#131
post #114

Earlier quoted context omitted.

> A stateful function with effects. Your language just doesn’t have a primitive to express it. I wonder what about generator functions?

I don't see how generators would change anything here. "with effects", "stateful", and the integration between the two are all equally important in the statement.

Well generators are a good primitive to represent "stateful" "functions" "with effects". The concept of hooks, which somehow give the control back to React to do something, map marvelously to react.

I'd be surprised if generators didn't come into React at one point or another

Re: A Critique of React Hooks

#132
post #117
post #96

Earlier quoted context omitted.

I am far more confused by hooks than by classes and 'this'.

Also you can't really try to protect JavaScript developers from understanding this . It's a fundamental part of the language.

Disagree. I understand `this`, but I very, very rarely encounter situations where it doesn't mean the same thing as Java's `this`. When you work on a full-React codebase, it just doesn't really happen. We used to have weird `this` behaviour on event handlers, but arrow functions and hooks fixed this.

Not saying it's good or bad, just an observation

Re: A Critique of React Hooks

#133
post #83

Earlier quoted context omitted.

> A stateful function with effects. Your language just doesn’t have a primitive to express it. I wonder what about generator functions?

Yep, there was even a library making the rounds recently that leverages them: https://crank.js.org/

Another example: Redux Saga has been using them in React for a long time. https://redux-saga.js.org/

Re: A Critique of React Hooks

#134
post #96

Earlier quoted context omitted.

It's right there in their intro to hooks in the section "Classes confuse both people and machines" [0]. [0] https://reactjs.org/docs/hooks-intro.html#classes-confuse-bo...

I am far more confused by hooks than by classes and 'this'.

`this.foo = this.foo.bind(this)` and then the new feature of method properties `foo = () => this` vs `foo() { this }` are constant sources of confusion for people who aren't well-established in Javascript/this. Not to mention function() vs arrow functions.

All exist because of the idiosyncrasies of `this` that you may be discounting because of your familiarity and already being over the learning curve that ensnares people daily.

Re: A Critique of React Hooks

#136
Hooks have unlocked so much power in React but still deserve critiquing. However I think the author only hinted at the major complaint I have about hooks, which is that it's no longer Javascript. It's not a function, it's sort of like type system magic. Hooks can't be nested, order matters, can't be conditionally called, and you have to understand trickier memoization to avoid bugs. It also isn't portable knowledge to other systems, like vanilla Javascript is. React is great because it's vanilla Javascript, expressions all the way down, and lifecycle methods, which everyone is used to. Hooks are a new non-vanilla-javascript paradigm with special and sometimes tricky rules. Other than, there's no reason to write React unless you're using hooks, and I wonder what the next major paradigm shift will be. I look back on all our HOCs and function as children and shudder compared to how easy it is with hooks.

Re: A Critique of React Hooks

#137

Hooks have unlocked so much power in React but still deserve critiquing. However I think the author only hinted at the major complaint I have about hooks, which is that it's no longer Javascript. It's not a function, it's sort of like type system magic. Hooks can't be nested, order matters, can't be conditionally called, and you have to understand trickier memoization to avoid bugs. It also isn't portable knowledge t…

> React is great because it's vanilla Javascript

What about JSX? It’s very useful but it’s also an absolutely huge departure from vanilla JavaScript and hides a fair amount of complexity behind what your code is actually doing.

Re: A Critique of React Hooks

#138
post #137

Hooks have unlocked so much power in React but still deserve critiquing. However I think the author only hinted at the major complaint I have about hooks, which is that it's no longer Javascript. It's not a function, it's sort of like type system magic. Hooks can't be nested, order matters, can't be conditionally called, and you have to understand trickier memoization to avoid bugs. It also isn't portable knowledge t…

> React is great because it's vanilla Javascript What about JSX? It’s very useful but it’s also an absolutely huge departure from vanilla JavaScript and hides a fair amount of complexity behind what your code is actually doing.

My mental model of JSX is that it's vanilla Javascript, and it's helped me appreciate trying to write more expressions and less statements. Like JSX isn't getting transformed into some weird different control flow, it's just a nicer way to write the same expression. And DSLs are a good general computer science principle. Hooks don't seem quite as general of a concept to me as a really well thought out DSL that has a minimal surface area but still turns out to be super useful.

Re: A Critique of React Hooks

#139

Hooks have unlocked so much power in React but still deserve critiquing. However I think the author only hinted at the major complaint I have about hooks, which is that it's no longer Javascript. It's not a function, it's sort of like type system magic. Hooks can't be nested, order matters, can't be conditionally called, and you have to understand trickier memoization to avoid bugs. It also isn't portable knowledge t…

> I look back on all our HOCs and function as children and shudder compared to how easy it is with hooks.

If by "function as children" you're referring to render props, personally I was really happy to see that short-lived fad die out. I don't think render props made things simpler.

Now if we can admit we never needed Sagas just to do some data fetching maybe we can burn that stalled-out old bandwagon, too :D

(Sagas are a powerful pattern, I'm sure someone here is about to reply about how they're making good use of them. But I'd bet 99% of people using the redux-sagas library could be doing something simpler).

Re: A Critique of React Hooks

#140
post #114

Earlier quoted context omitted.

> A stateful function with effects. Your language just doesn’t have a primitive to express it. I wonder what about generator functions?

I don't see how generators would change anything here. "with effects", "stateful", and the integration between the two are all equally important in the statement.

There are two different concepts as I see it, please correct me if your definitions differ.

"stateful": A function that has state, i.e. can store data

"with effects": A function that modifies data outside its own scope

Normal JS functions (as opposed to arrow function) already do this:

function foo(){};

foo.state = bar;

Generator functions take it a step further, where the function remembers internal state between calls.

Post reply on HN