5 ms·
These are difficult to understand concepts imo. I have written an annotated implementation of a simple reactive system (you may want to run it through Prettier
by fabiospampinato 4y ago
These are difficult to understand concepts imo. I have written an annotated implementation of a simple reactive system (you may want to run it through Prettier if that's your thing): https://github.com/fabiospampinato/flimsy/blob/master/src/flimsy.annotated.ts https://github.com/fabiospampinato/flimsy/blob/master/src/fl...
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.
- remram 4y agoBut your description matches useState exactly...
- thelastbender12 4y agoSorry, you seem to hint at knowing this already. But, to clarify, * Hooks - `setState` reruns the full code for your component. * Signals - only the dependent effects/computed/JSX is run.
- remram 4y agoIf the runtime is capable of this, why doesn't it do it for setState? 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.
- thelastbender12 4y agoIf you are posing the question, "why does Preact not make hooks behave like signals?", it very likely is to retain similarity with React where you only have hooks. For better or worse, Preact is known to most folks as an alternate implementation of React with the same API. 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.
- remram 4y agoBut the API of signals and hooks is the same... What would change if new signal thing was called useState?