3 ms·
> hooks use closures for state and if you call hooks in wrong order you get the wrong values back. Calling hooks in wrong order gets the wrong value back? Can
by flowerlad 7y ago
> hooks use closures for state and if you call hooks in wrong order you get the wrong values back.
Calling hooks in wrong order gets the wrong value back? Can you explain this one?
- rossta 7y agoHooks rely on a persistent call index. Here are a few posts that explain in more detail: https://overreacted.io/why-do-hooks-rely-on-call-order/ https://overreacted.io/why-do-hooks-rely-on-call-order/ https://github.com/reactjs/rfcs/pull/68#issuecomment-439314884 https://github.com/reactjs/rfcs/pull/68#issuecomment-4393148...
- TomMarius 7y agoBut why would you even change the order? I don't understand how can this be an issue if you treat it as a static declaration which it simulates.
- rssbnjmnfrncs 7y agoWhat if you wanted to return early in the component? E.g. a hook has returned a certain value and you now want to stop all other execution and return null. With hooks you can't as that would change the order.
- TomMarius 7y agoYeah but you also wouldn't try to return in the middle of function argument declarations. I know it's a little weird, but just treat it as a declaration block and you won't have any problems, what you're proposing is impossible with react architecture as a whole anyways (because the next component would get wrong state cells, it's a stack).
- draw_down 7y ago"Calling in the wrong order" is not an actual concern when writing React code. What you actually need to do is make sure that they aren't called conditionally. (For example, if your function checks some condition and then returns null, that must happen after any hooks are called.) This would be annoying if you had to do it yourself, but that's what linters are for. Ends up not being a big issue in practice.