Live data from Hacker News

Why Do React Hooks Rely on Call Order?

overreacted.io

91–100 of 115 posts

Re: Why Do React Hooks Rely on Call Order?

#91

I think this post has actually pushed me back towards Symbol keys perhaps being a good idea. The useFormInput() example under Flaw 3 seems rather contrived – wouldn’t you just pass a Symbol key to useFormInput and it would then pass the key to useState, solving the supposed flaw? If you had to use useState several times in useFormInput, just use a WeakMap (they’re not that scary) with the keys being the Symbols passe…

i think the problem with the naive symbol approach is you need to know all the keys the user hook wants to use and pass them all in which is not really scalable. however, instead of using a single symbol you could use an array of symbols which would solve that problem. a user hook would take in an array of symbols from its caller which would define its namespace then it would add a symbol to this namespace to create a new array for each nested call it wants to make.

the example would become something like:

    const nameNamespace = [Symbol()];
    const surnameNamespace = [Symbol()];

    const name = useFormInput(nameNamespace);
    const surname = useFormInput(surnameNamespace);


    const valueKey = Symbol();
 
    function useFormInput(namespace) {
      const [value, setValue] = useState(extendNamespace(namespace, valueKey));
      return {
        value,
        onChange(e) {
          setValue(e.target.value);
        },
      };
    }
there are a lot of drawbacks with doing it like this tho. like it is much less performant because you are creating all these arrays and having to do these comparisons across possibly big chains of symbols. also, there doesn't seem to be any way to easily store these chains in a map like structure for fast lookup except for using nested maps which is a bit weird.

Re: Why Do React Hooks Rely on Call Order?

#92

Earlier quoted context omitted.

While I agree with you, I think it's important to maybe specify that visual frontend builders generally suck -- for web frontends. In general, the visual UI builders for mobile apps tend to be very good. The reason I think it's important to mention this is because I believe that the amount of variability you have to deal with in the web is far greater, which is why I believe these visual frontend builder systems tend…

Visual UI builders for mobile cannot even handle reusable components directly. (Similar dialogs via mixin or inheritance, style extraction etc.) Custom widgets? Amount of work to get them running in one of these is a nightmare. Compare that to game engines such as Unity or UDK and their editors, it's so bad it's laughable.

Why do you think anyone haven't built a good one yet?

Re: Why Do React Hooks Rely on Call Order?

#93

I think this post has actually pushed me back towards Symbol keys perhaps being a good idea. The useFormInput() example under Flaw 3 seems rather contrived – wouldn’t you just pass a Symbol key to useFormInput and it would then pass the key to useState, solving the supposed flaw? If you had to use useState several times in useFormInput, just use a WeakMap (they’re not that scary) with the keys being the Symbols passe…

i think the problem with the naive symbol approach is you need to know all the keys the user hook wants to use and pass them all in which is not really scalable. however, instead of using a single symbol you could use an array of symbols which would solve that problem. a user hook would take in an array of symbols from its caller which would define its namespace then it would add a symbol to this namespace to create…

This is how I would have hypothetically done it with WeakMaps: https://gist.github.com/thomasfoster96/c4a20053c747196f027fc...

Re: Why Do React Hooks Rely on Call Order?

#94
post #81

Earlier quoted context omitted.

> 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 personall…

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 mix…

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?

#95

Earlier quoted context omitted.

In actual use I've seen that Redux apps often wind up with a lot of accidental data dependencies that probably wouldn't have happened without a centralized store. For example devs will lazily use a "currentUser" key in the store for all kinds of unrelated stuff and subtle bugs creep in. Another common problem with Redux is memory issues because various components don't clear their data out of the store when they're u…

I absolutely agree with you and that's been my experience too, I've seen exactly those problems in both my own code and others' code. It took me personally many iterations over various projects to learn the boundaries of where redux makes more sense and where component state makes more sense. That's perhaps the biggest actual inherent problem with redux, that it may implicitly encourage everything to be in the one si…

Hi, I'm a Redux maintainer.

The Redux FAQ specifically has an entry with rules of thumb to help decide when it makes sense to keep a given piece of state in Redux [0].

I do agree that the "Single Source of Truth" principle [1] is probably over-interpreted, and maybe needs some caveats somehow. That said, it's tough to simultaneously say "here's the basics of how Redux works", "here's the ideas behind why you _should_ use Redux", and also try to tell people when to _not_ use Redux.

We're currently planning a revamp of the Redux docs content [2]. I'd appreciate it if you could fill out this survey on how we can improve the docs structure [3], or leave a comment in that issue thread with some suggestions.

[0] https://redux.js.org/faq/organizing-state#do-i-have-to-put-a...

[1] https://redux.js.org/introduction/three-principles

[2] https://github.com/reduxjs/redux/issues/2590

[3] https://docs.google.com/forms/d/e/1FAIpQLSfzIkY3fXZ8PrQKScYM...

Re: Why Do React Hooks Rely on Call Order?

#96
post #17

Earlier quoted context omitted.

On the contrary: Redux's insistence on serializable events and side effect-free reducers makes debugging these pernicious "state update" ordering issues, which exist with or without Redux, a far easier job.

Agreed. I like to tell people that I like Redux's devtools more than I like Redux. There's a payoff for all the boilerplate.

Please check out our new `redux-starter-kit` package, which includes utilities to simplify common use cases like store setup, defining reducers, immutable update logic, and even creating entire "slices" of state automatically:

https://redux-starter-kit.js.org/

Re: Why Do React Hooks Rely on Call Order?

#97

Earlier quoted context omitted.

Dan, thanks for your work on React and all the new features from you and the team. I love your stewardship of this project. React is fantastic and I’ve loved your choices of features to add. If you could just make docs less verbose...

Then other people will ask to make them more detailed :-) Thanks for feedback though, we’re listening.

[deleted]

Re: Why Do React Hooks Rely on Call Order?

#98

Earlier quoted context omitted.

How do custom Hooks look in this world?

Maybe something like this? https://gist.github.com/sebastiaanvisser/72e6bc54baa14abc08e... It all seems to boil down to packing and silently composing lifecycle methods. Either with classes, records of functions, or effectful functions, dictionaries. I can see how the ergonomics of current react hooks are actually great, but I still think they’re weird :) I’ll probably get over it some day. edit: note how hooks are n…

How would you pass values between two different Hooks? See my two examples in the post (useSpring and useFriendStatus).

Note they need to be always up-to-date and not just execute once.

Re: Why Do React Hooks Rely on Call Order?

#99
post #94

Earlier quoted context omitted.

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 mix…

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 func…

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?

#100

Earlier quoted context omitted.

Here’s what I mean: https://gist.github.com/thomasfoster96/c4a20053c747196f027fc... > A key design goal is that creating a custom Hook is easy. You should be able to literally copy paste part of your component (e.g. a bunch of useState calls and some event handlers) and call it a day. I'd totally understand that reasoning, because the keyed Hooks are more verbose and would generally require two or three parts of a co…

It is definitely possible but there’s so many more places you could make a mistake if each custom Hook has to do bookkeeping like this. I don't know if I could write or extend the code you wrote without making quite a few mistakes in the process. By comparison, I find following a rule like "calls should be static" much simpler. My post does mention that we care about copy paste experience: >Code passing non-unique or…

> It is definitely possible but there’s so many more places you could make a mistake if each custom Hook has to do bookkeeping like this. I don't know if I could write or extend the code you wrote without making quite a few mistakes in the process. By comparison, I find following a rule like "calls should be static" much simpler.

The book keeping could be moved into a couple of utility functions - it’d be largely the same for most custom hooks.

I’m also not sure relying on a linter is necessarily going to make static call Hooks simpler. Poorly written hooks are going to be buggy whether they’re Symbol keyed or not. I think that one of the big disadvantages of static call Hooks would seem to be that incorrect conditional usage could still accidentally work.

> My post does mention that we care about copy paste experience

I think I misunderstood that section when I first read it - it makes sense now. I’m not convinced it’s a huge win though.

> I later go into why allowing conditional declarations of state or effects isn’t even particularly useful or desirable because the semantics are too confusing.

We’ll have to agree to disagree - while I’m not eagerly wanting to use Hooks in conditionals, I dont think the semantics are that confusing.

Post reply on HN