Is a signal just a useState hook?
I’m sure the answers are no. But I’m struggling to see it in the provided examples.
Is a signal just a useState hook?
I’m sure the answers are no. But I’m struggling to see it in the provided examples.
A signal is basically a function that you have to go through to read and write a value. In the case of Preact the function is split into getter and setter assigned to the "value" property. The interesting thing about signals is that they can tell their parent computation to re-execute, automatically, without any manual dependency array.
A computed is a signal generated from a function rather than a primitive. So like the function that generates the value is re-executed automatically whenever any of the signals read inside it change.
* Hooks - `setState` reruns the full code for your component.
* Signals - only the dependent effects/computed/JSX is run.
What I'm complaining about is that the signals seem to have the exact same semantics and use cases with different implementations and performance characteristics.
SolidJs shows you can get most of the functionality of hooks using signals. Who knows, if someday most Preact users converge to using signals for state management, maybe they will throw out hooks altogether. But in their current context, supporting both hooks and signals feels a reasonable choice to me.
EDIT: scroll up to "The global state struggle" first to get more background/comparison to useEffect if it isn't clear.