4 ms·
We'd probably have to talk about it IRL; the question you're asking kind of implies you're coming at the whole thing from the wrong direction, and so any answer
by johnfn 1y ago
We'd probably have to talk about it IRL; the question you're asking kind of implies you're coming at the whole thing from the wrong direction, and so any answer I give would be unsatisfying unless I could somehow transfer the entire philosophy in my head to you.
The short answer is that what you're asking doesn't really make sense to me. I think of the lifetime of state as analogous to member variables of a class. In the same way that member variables in a class are always visible to the entire class, a state variable in React is always visible to the entire component. You wouldn't want it to be scoped to an if block any more than you'd want a member variable to be scoped to an if block.
Maybe now you're thinking "well, that's limited in an annoying way, and Svelte is better". Maybe. But I suspect for any problem that exists that you think needs conditionally-scoped state, there's a nice clean solution in React. That's what I suspect, anyways. But I very rarely run into problems in React that I can't express in nice, clean code.