Hacker News new | ask | show | jobs
by dartos 629 days ago
I thought pre-hooks react provided a really nice and simple abstraction over how the web works.

FRP is a nice fit for the kind of UI that makes the majority of the web.

Hooks ruined the framework imo. Confusing api (useState returning an array for example).

Having a class component with hooks for lifecycle behavior made perfect sense. Each component’s state being a field on those classes was easy to understand.

Hooks came out of left field and made everything more complex for the worse.

3 comments

> Hooks ruined the framework imo. Confusing api (useState returning an array for example).

It just returns a tuple. That's not confusing at all and it's a very well established pattern in most modern languages.

If there's anything that can be considered complex and footgun-y in React it's useEffect because most of the time you shouldn't use it at all but it can be abused very easily and it kinda works even if you abuse it (but introduces a huge maintenance burden).

> It just returns a tuple. That's not confusing at all and it's a very well established pattern in most modern languages.

A few things wrong with this. It does not return a tuple, it returns an array with 2 elements. You, as the react dev, need to just know that this is the case and that one is the value and the other is a set function.

In most modern languages, tuples are common, yes. But not in JavaScript. React isn’t a python or go framework, it’s a JavaScript framework. Why doesn’t it act like almost everything else in js land and return an object with 2 named fields? Why place extra knowledge burden on the developer? It’s just poor api design

Yes useEffect is also a huge complex pain point, but it just takes longer to type out why that is.

Pretty much all the hooks are as almost nothing else in the JavaScript ecosystem acts like they do.

> Why doesn’t it act like almost everything else in js land and return an object with 2 named fields

The explanation is actually very simple: because in 100% of times you need to give those two fields custom names. It is easier and more concise with tuple:

  const [text, setText] = ...
Than with object + renaming, which quickly gets clumsy when you have dozens of such lines in your component:

  const {value: text, setValue: setText} = ...
That’s not a good reason.

Especially in our world where strong typing is something preferable.

Fewer characters at the cost of clarity is poor api design. Implicitness is almost always worse than explicitness.

Explicit !== clumsy

And again, almost nothing else in the js ecosystem behaves like this. Especially at the time of the big hooks update to react.

Also, again, it’s not a tuple. It’s an array with 2 elements. JavaScript does not have tuples or the semantics for interacting with them as such. You just need to know that there’s always going to be 2 elements.

That extra knowledge burden is the hallmark of poor api design.

> Also, again, it’s not a tuple. It’s an array with 2 elements. JavaScript does not have tuples or the semantics for interacting with them as such

Not true. In TypeScript, there are tuples — you can perfectly describe an array having 2 elements with different types, and even give names to them:

  type KVPairTuple = [key: string, value: number]
We use it all the time for simple 2-element thingies. You can say JavaScript isn't TypeScript, but cmon, just everybody out there uses TypeScript, it is the basic norm nowadays. So we're really talking about TypeScript semantics here.

Also, even JS uses tuples for its core APIs. Enter `Object.entries` and `Object.fromEntries` — they operate on kv-pairs in form of 2-element arrays. So your assertion that tuples are something alien to JS ecosystem isn't accurate simply because of that.

> Not true. In TypeScript…

Not talking about typescript.

Typescript wasn’t nearly as wide spread or as complete back then anyway. Facebook was still on flow.

> they operate on kv-pairs in form of 2-element arrays.

So… Not tuples…

> So your assertion that tuples are something alien to JS ecosystem isn't accurate simply because of that.

My words were “almost nothing else.” I’m sure someone somewhere uses the idea of tuples in their js code (implemented as arrays, ofc) but it’s extremely far from the norm.

Also none of that refutes my point that it’s poor api design. Thanks for being pedantic tho.

> Yes useEffect is also a huge complex pain point

We have a rule of thumb in our projects: if you use useEffect directly in application code, it is a probable sign that something is wrong with your code, like you're working on a wrong level of abstraction. Almost every direct use of useEffect is better solved with some standard hook from a popular library (like react-use or whatever you like — there are plenty of them).

Like, binding event listeners? useEventListener

Network/async calls? useAsync

timeouts/intervals? useInterval, useTimeout

And etc. etc.

useEffect is really a low-level building block for library code and we rarely use it directly, as it is unsafe and hides the original intent. Much like `new` and `delete` in C++ — you don't use it directly, there are smart pointers. Library hooks are your safe smart pointers over useEffect.

React + classes made sense. Redux was hard to use. All react needed was a simple state management solution that didn't take 500 loc across multiple files and a ton of useless object copying to make a page full of form elements work.

Instead it got a complete paradigm shift to a leaky abstraction. Not that the previous abstraction wasn't leaky, it was alwayz super easy to have components re-render when not necessarily.

There's a world where browser UIs are programmed in Smalltalk instead, which I think is what the authors of the DOM may have intended when building it out OOP-style.

It would bear almost no resemblance to the web we have today, but message passing between UI components throughout various parts of their lifecycle has been a great way to model an interface for decades. You only need to look at the overall fit and finish of a typical MacOS or iOS app, with Swift simply pulling Objective-C into the 21st century.

Web UIs tend to make heavy use of message passing via event listeners.

The major difference between web UIs and desktop apps is two fold. In web UIs, you don’t generally have an event loop in which you change your app state based on system input events and there is, generally, a slow request/response cycle for any interaction.

That’s why immediate mode UI doesn’t really fit well in web land.