3 ms·
I'd be wary to encourage useState. It seems too easy to accidentally use in loops/callbacks/etc where your state would then break because react tracks it via ca
by snek 8y ago
I'd be wary to encourage useState. It seems too easy to accidentally use in loops/callbacks/etc where your state would then break because react tracks it via call order instead of call site.
- sophiebits 8y agoThe rule about unconditional calls tends to sound weird (I personally found it very strange when I first saw it), but in my experience, people do not have a hard time following it – it’s very rare to want a variable number of state fields, for example. If you do end up making a mistake, React has both lint rules provided and runtime checks to catch any errors. But from watching many people learn and use Hooks, we haven’t seen it to be a problem in practice.
- littlecranky67 8y agoYou don't use useState to set the state, you use useState() to GET back a function that can mutate the state. And this function is safe to call in a loop or conditional. When using hooks, yes, you have to remember not to call any use* function inside loops or conditionals (or use the linter plugins for that). Besides that, hardly any room for errors.