4 ms·
> React assumes that if you call useState many times, you do it in the same order during every render. Note they also say: > We provide a linter plugin to enf
by kiliancs 8y ago
> React assumes that if you call useState many times, you do it in the same order during every render.
Note they also say:
> We provide a linter plugin to enforce these rules automatically.
Makes me feel a little better.
- maaaats 8y agoWhy couldn't one just pass in `this` to useState, a key, or something similar? Like, if I have two state variables, pass in a name for both, instead of relying on the order being the same.
- adriancooney 8y agoI doubt the linter will enforce the same-ordering, that would require state between runs. I think the linter just ensures hooks are run at the top level in the functional component.
- atomwaffel 8y agoAnd it wouldn't need to. You're free to move around calls to useState() to your heart's content in your code, you just need to call them in the same order during the lifecycle of your component.