3 ms·
You can write const [a, b] = useState('x') in vanilla js and typescript. Hence it is not magic syntax.
by snemvalts 1y ago
You can write const [a, b] = useState('x') in vanilla js and typescript. Hence it is not magic syntax.
- rictic 1y agoYeah, that's vanilla syntax. The semantics are fairly magic though. The component function that calls useState though isn't a normal function, it's a function that must be called in a special way by the React runtime in order to line up all of the hidden state that React maintains so that it can magically infer the data that your `useState` call maps to and then there's more magic to maintain the lifetime of that data.
- whoknowsidont 1y agoSome of you are really going out of your way to make this more complicated than it is. Next you're going to complain about the fact that the compiler handles static variables with controlled allocations for you in C. Like there's no rational basis for your complaints or critique other than grandstanding.
- snemvalts 1y agoYes, but it is not syntax. It's a contract with the library. React is completely usable using vanilla JS syntax. Same cannot be said for Vue and Angular. It feels a bit like talking about apples and oranges in this thread.
- conartist6 1y agoYour example usage only implies what I would consider the non-magic implementation behavior. I could fulfill that contract with `(initial) => { let s = initial; return [ s, v => s = v]; }`. No hidden magic there, and no chance of breaking referential identity.