Re:React I love you, but you're bringing me down
by bigDinosaur on Wednesday September 21, 2022 @12:27AM
(#32920792)
Classes in JS are just functions anyway, and functions can have state. Saving the state of a function is a different thing, but of course JS allows that, too. Why is it weird?
[ Parent
| on HN ]
Re:React I love you, but you're bringing me down
by wiredearp on Wednesday September 21, 2022 @03:17AM
(#32921740)
Class instances are objects and it's the objects that have state. Functions can only abuse themselves as objects to store state.
function johnson() { johnson.state = {}; }
This is however shared state and the state will be reset whenever the function is called a second time. So what you normally do with a function is to pass the state via arguments and now the same function can work with different states. React could totally do this via props or as a second argument.
function Johnson(props, state) {}
Instead they changed the laws of functions so that a function can act different the first time it is called and also be called the first time multiple times if and when the function calls a functions that looks like JavaScript but follow different rules [1].
This is weird.
[1] https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at...
Re:React I love you, but you're bringing me down
by bigDinosaur on Thursday September 22, 2022 @05:24AM
(#32936566)
As far as I know, hooks use closures to store state, and that's neither abuse nor weird. Am I missing something?