News for hackers. Stuff that matters.
Re:Why Do React Hooks Rely on Call Order? by danabramov on Wednesday December 12, 2018 @09:43PM (#18669483)
(I edited the post title to include “React” before the “Hooks” to disambiguate. Might be worth editing the submission too!)

Hope you’ll enjoy reading it.

Re:Why Do React Hooks Rely on Call Order? by strife25 on Wednesday December 12, 2018 @10:59PM (#18669757)
Just want to say that I've been loving your posts so far. Well written and I love the perspective of exploring _why_ an API is designed the way it is instead of trying to focus on explaining why <insert library> is the best for <insert language>. There are only a handful of blogs I've read over my career that I feel like focus on solving real application development problems, exploring trade-offs, or digging into why an approach was taken and these posts are of that caliber. Those topics help me in the problems I encounter frequently in my day job and make me a better engineer.

Thank you for the time and effort you're putting into this.

Re:Why Do React Hooks Rely on Call Order? by kmarc on Thursday December 13, 2018 @02:27AM (#18670480)
I just briefly looked at the examples (and of course followed the twitter boom about it), but what I am not getting is that to me it looks like since useState() is against the principles of pure functional programming.

I understand that it implements some kind of inversion-of-control, but just looking at the code it feels like using some global object's method which is a big no-no. Also this importance of ordering reminds me the unmaintainable magic hell of Angular.

Maybe I'm just missing the "explicit-over-implicit" concept here. What's your opinion on this?

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @08:21AM (#18672081)
I suggest you to read about algebraic effects. We don't have them in JavaScript, but conceptually that's what Hooks represent. (This is as functional as it gets.)

React has always been about taking useful ideas from functional programming and bringing it to mainstream through pragmatic choices in JS.

Your concern about the "globalness" is addressed in Sebastian's comment which is linked five times throughout the post — you should definitely check it out! https://github.com/reactjs/rfcs/pull/68#issuecomment-4393148...

Finally, don't forget you're comparing Hooks to classes. Those are hardly functional either.

Re:Why Do React Hooks Rely on Call Order? by asark on Thursday December 13, 2018 @08:45AM (#18672240)
It's not a global object—it's an instance of the object for the component you're using. The whole thing's a terrible OOP system in a language that already has a built-in mediocre OOP system, which terrible OOP system is, in the end, just a(n inefficient) pass-through to same built-in mediocre OOP system.

The posters here wondering why you can't name them are on to something. I assume it's because then it'd be too obvious that's what they're doing, and whoever's paying people on that team (I really hope it's not more than one person, it's not hard work, but it probably is) might notice and make them stop, and maybe the React team at FB would even shrink in size, and we can't have that.

I'm not sure what other explanation there could be for such comically-wasteful sandcastle building. I assume it's a combination of individual incentives to work on something not-difficult but flashy and prominent, with project incentives to never need fewer people than they currently have.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @08:57AM (#18672310)
>The posters here wondering why you can't name them are on to something

Can’t name what? Not sure I follow.

>I really hope it's not more than one person, it's not hard work, but it probably is

We had from 5 to 8 people on the team at different times. Maybe maintaining one of the most popular open source projects isn’t “hard work” for you but we find it challenging.

>I'm not sure what other explanation there could be for such comically-wasteful sandcastle building.

We try to solve problems that product engineers run into. If you have better ideas we’d love to hear them.

Re:Why Do React Hooks Rely on Call Order? by asark on Thursday December 13, 2018 @09:20AM (#18672435)
>The posters here wondering why you can't name them are on to something

Imprecise phrasing on my part, the discussion here has been around naming hooks with Symbols.

> We had from 5 to 8 people on the team at different times. Maybe maintaining one of the most popular open source projects isn’t “hard work” for you but we find it challenging.

I meant React Hooks specifically, not Redux, which I assume is what you mean here.

> We try to solve problems that product engineers run into. If you have better ideas we’d love to hear them.

Use the built-in OOP system instead of writing a worse new one? Reinventing methods and properties with poor, misleading syntax as a thin layer over the OOP system of the host language is... well, it's helping bloggers, I guess.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @09:54AM (#18672676)
I’d love to hear about your solutions but maybe don’t be so quick to dismiss other people’s hard work as something to “help bloggers”. If we were chatting face to face would you also behave like this?

We’re very open to good technical arguments but this isn’t one.

Re:Why Do React Hooks Rely on Call Order? by asark on Thursday December 13, 2018 @10:17AM (#18672839)
> If we were chatting face to face would you also behave like this?

Maybe? I usually very much would not, of course, but JS "culture" creates significant irritation and wasted time for me daily, and has for years, especially in React-land, since that's where the money is lately so it's hard to avoid. The only other software that gets me this exasperated is anything Poettering thinks up, but at least I don't personally have to work closely with any of that daily (any more, and for now).

I'm sure I'll have to deal with hooks when people around me start using them. It's less that I'm upset that they exist than I feel like I'm being gaslighted. I've read the docs, and the source, because, again, I have to know this stuff. After the initial disbelief wore off I had a good laugh, like, actual LOL. This thread is the first of many I at that moment predicted I'd see as people who really, really in their hearts and in their actual technical needs and in the language they're using, just needed OOP, but are on the JS-must-be-functional-at-all-costs hype train, expressing frustration and confusion over this feature and burning lots of time trying to sort out how OOP works when you're trying so hard to not type "this" and pretending it's something else. It's a less-useful and confusing replacement for OO that re-implements just enough of it that people will 100% for sure hang themselves with it, and omits enough that people will complain about it. It's a perfect device for generating confusion. It's going to be used for no benefit, or misused harmfully, a ton, and meanwhile everyone's gonna be very confused about it.

Ditto Redux—which is at least not funny in itself the way React Hooks are, though the flailing and consternation around it are—which is dead simple if you don't use the wrong words to describe things, yet how many person-hours have been lost trying to decipher what's going on there? So, so many. On the one hand it's funny, and I do legitimately enjoy all the accidental humor in React land. On the other I have to console, counsel, and train the folks who run into difficulty with this stuff, and live with or fix software in which it's been misunderstood and misused.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @10:23AM (#18672891)
I empathize with your frustration of having to work with something you don’t like (or feel is unnecessary). I would much prefer that people who don’t like React aren’t forced to use it but that’s not how job market works. Sorry about this.

I’d love to see what OOP solution you envision for these problems. It’s not like we’re unfamiliar with OOP. In fact the OOP version of Hooks is what we had before. It’s called mixins. Mixins, like other forms of multiple inheritance, suffer from the “diamond problem” described in the post, and many others:

https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamo...

https://reactjs.org/blog/2016/07/13/mixins-considered-harmfu...

If you have an OOP solution that solves the same problems Hooks solve, but without the downsides of mixins, I’d love to hear it. You’re being vague in your proposed fix which makes it difficult to discuss.

I also want to emphasize we’re not “FP purists”. (In my opinion some codebases take FP way too far making the code very difficult to follow.) In many ways, Hooks help replace those heavy-handed patterns. So I think you might actually like them if you spend some time using them.

Re:Why Do React Hooks Rely on Call Order? by asark on Thursday December 13, 2018 @11:08AM (#18673345)
I entirely share your "ew, gross, no" re: mixins generally. That their being added to React in the first place is part of this history is definitely interesting.

Thing is, I don't even consider Hooks not OO. They're just a really limited in-JS partial re-implementation with bizarre syntax. React tracks your "this" for you so it can dispatch the calls correctly. Your constructor gets mushed around in your render function for some reason. But it's attaching properties and methods to an instance. It's going to require care and discipline to use it correctly, given its quirks, so just direct that same discipline toward composition-over-inheritance instead, is my thought, which you can do without yet another way to write things. The fix is quit hitting yourself, in short, but if you don't I guess we'll hand you another way to hit yourself, but differently? This is just one more layer of complication that everyone's now got to understand (or, more likely, not, but use anyway) to even read other people's React codebases.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @11:35AM (#18673602)
Sorry but this is still pretty hand-wavy :-) I’d be happy to discuss a specific “before” and “after” code example.

There’s neither methods nor properties in Hooks code. I think you might be doing the same thing you think we are doing — you’re projecting the API you see onto the metaphors that feel more familiar to you. But these metaphors don’t really match what the API is doing or what it represents.

But again, this discussion is fruitless without specific code examples to anchor it.

Re:Why Do React Hooks Rely on Call Order? by asark on Thursday December 13, 2018 @05:50PM (#18676864)
> There’s neither methods nor properties in Hooks code. I think you might be doing the same thing you think we are doing — you’re projecting the API you see onto the metaphors that feel more familiar to you. But these metaphors don’t really match what the API is doing or what it represents.

I mean that Hooks implementation re-implements key elements of a typical implementation of methods and properties, and end up mimicking them in important ways, differing mainly in the parts of that it doesn't include. Not that it actually uses methods and properties (though it does, of course—the "current component" that React tracks is an object, and the Hooks code leans on that to determine its calling context).

> you’re projecting the API you see onto the metaphors that feel more familiar to you.

Place a typical OO implementation next to what Hooks are doing, and you don't even have to squint to see that they're quite close. The porcelain's super-weird, yes—magical in all the "wrong" places, explicit in all the "wrong" places, missing a ton of mostly-inheritance-related stuff—but even with all that a Render function using Hooks manages to look an awful lot like a class declaration, as if someone had implemented classes in a language but forgotten to add any of the syntax to support it.

EDIT: I mean, seriously. The source is public. It's a really fun read.

https://github.com/facebook/react/blob/master/packages/react...

Re:Why Do React Hooks Rely on Call Order? by politician on Thursday December 13, 2018 @02:01AM (#18670382)
Looking at the design, it's clear that the team is trying really hard to make functions look and work like classes look. Variable declarations at the top, followed by helper methods, followed by lifecycle methods. Given that classes are themselves sugar to make prototypes palatable to a broad range of developers, it's certainly a tough design task; I'm not convinced that reliance on call-index is wrong per se, but it is magical (and magic in JavaScript is obnoxious).

That said, the presentation of the third and forth flaws seem a bit weak. useState accepts a key as the first parameter, but in both of the composite functions that input parameter disappears. That looks like a refactoring error.

From a design standpoint, if `useState(symbol)` is acceptable (and I'm not saying it is), then `useWindowWith(symbol)` would likewise be acceptable in that it's not adding any more requirements to the interface than the vanilla version.

Proper bookkeeping of symbols resolves the issues here. However, if the design objective is to reduce the effort on the developer to do their own bookkeeping, then call-order indexing does make sense.

(Not to mention that Symbols are not supported by IE11 which remains an important target for bigger companies.)

I'm not sure there's a good way out. When you look at solutions like Protocol Buffers, they just bite the bullet and require the developer to supply the indexing. If JavaScript had something like Go's iota, then you could imagine using an enumeration to supply the indexing without requiring everyone to type 1,2,3,4... etc. But it doesn't so, that's wishful thinking. Nevertheless, the react codebase itself does contain a giant list of assignments of Symbol() || number to constants, so it's a pattern you're already aware of.

A tough nut to crack.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @08:22AM (#18672092)
>From a design standpoint, if `useState(symbol)` is acceptable (and I'm not saying it is), then `useWindowWith(symbol)` would likewise be acceptable in that it's not adding any more requirements to the interface than the vanilla version.

I think you're missing that `useWindowWidth()` could have more than one `useState()` and thus you'd need to somehow compose Symbols. Which is what the next section is about.

Re:Why Do React Hooks Rely on Call Order? by danabramov on Thursday December 13, 2018 @08:30AM (#18672132)
In particular, I would suggest to take the `useSubscription` example from "diamond problem" section and try to convert that whole snippet. You'll see where it falls apart.
Re:Why Do React Hooks Rely on Call Order? by politician on Thursday December 13, 2018 @02:18PM (#18675127)
I must be missing something because this didn't fall apart when I refactored it to work with a `useState(symbol, value)` interface. I'm not saying this is a good or ideal solution, indeed if the useWindowWidth hooks were to both useState and also useSubscription, then multiple keys would have to be injected -- it would work correctly, but the interface would have the AMD feel that perhaps is undesirable (per flaw #8).

https://gist.github.com/politician/5f03c169a4119a63abb785b1c...

EDIT: I still think it's a hard design problem and the concerns in flaw #8 are _very real_ to a broad range of developers. When you're just trying to get it done, proper bookkeeping of Symbols and injecting them into hooks composed of other hooks might cross the line. It's a shame that relying on call-order indexing is the solution because it's magical. But at the end of the day, engineering is about trade-offs. Time will tell whether deeply nested composition of automatically-managed hooks was a good feature to expose.

Re:Why Do React Hooks Rely on Call Order? by WorldMaker on Thursday December 13, 2018 @05:12PM (#18676621)
> It's a shame that relying on call-order indexing is the solution because it's magical.

I've been thinking about this a lot since Hooks were introduced, and I'm increasingly of the opinion that it isn't that magical. Order of operations is incredibly important in the functions we write, especially in a language like JS that makes no effort for strict "pure" side-effect free functions. The order of a console.log or a return versus an increment matters in JS. We write a lot of procedural code in JS where order matters (a lot) already.

In that matter, Hooks can just melt into the "procedural" background of JS.

That said, I still feel like I want a better solution than "lint errors" for things like accidental branches of a Hook. I don't have any more of a proposal for how that would work or what that would mean than the article here, though, unfortunately.

Re:Why Do React Hooks Rely on Call Order? by politician on Thursday December 13, 2018 @05:25PM (#18676693)
> I still feel like I want a better solution than "lint errors" for things like accidental branches of a Hook.

The best I can come up with is Sweet.js (hygenic macros), but can you imagine what outrage that would provoke?

Re:Why Do React Hooks Rely on Call Order? by WorldMaker on Friday December 14, 2018 @12:37PM (#18682685)
Yeah, macros/DSLs might be a bridge too far for a lot of users.

I keep trying to figure out if there is a way to push it into a type safety problem in Typescript, at least. A compile error would be preferable to a lint error, even if not everyone uses a typing compiler like Typescript.

If there were some way that you could get use* functions to narrow to `never` in any branch or branch-like position, that would be cool. Unfortunately, I can't think of a natural way in the type system to do that that would work in even a plurality of cases. Which drops back to it needing to be a context-sensitive linter rule.

Re:Why Do React Hooks Rely on Call Order? by politician on Friday December 14, 2018 @01:41PM (#18683325)
You might be able to use ES6 annotations to mark use* functions such that a bespoke Babel plugin can assert that the the control flow AST only contains functions so annotated in non-branching positions.

Basically, lint, but at transpile time.

Re:Why Do React Hooks Rely on Call Order? by WorldMaker on Friday December 14, 2018 @07:18PM (#18686117)
Similarly, tslint operates best as a Typescript plugin and so lint rules in Typescript still can happen at transpile time and aren't necessarily that much different in practice from compiler errors. Still bugs me a little that there's not a higher-kinded type or pattern that I can come up with to do it "for real" in the compilation.
Re:Why Do React Hooks Rely on Call Order? by drekembemutombo on Thursday December 13, 2018 @07:08AM (#18671706)
Enjoying your posts, keep them up! Hooks look like a very powerful API for sharing pieces of functionality between components. You've probably heard this one a lot, but what bothers me a bit about hooks (especially useState) is that before, when you saw a component defined as a function, you could assume that it was just a function that renders something based on its props. However, I think it's a fair trade-off for such a powerful API.
Re:Why Do React Hooks Rely on Call Order? by WorldMaker on Thursday December 13, 2018 @02:20PM (#18675136)
Recent versions of React have encouraged truly pure props-only functions to wrap them with React.memo(), which also adds the benefit of giving them the equivalent of a shallow-check `shouldComponentUpdate()` avoiding re-renders when the props are the same.

React.memo() is all the more useful of a signal in the Hooks world.

Re:Why Do React Hooks Rely on Call Order? by rattray on Thursday December 13, 2018 @12:10AM (#18669980)
Thanks, this was fun. Enjoying your "unofficial" posts.