Signals are reactive primitives for managing application state in Preact.
State changes automatically update components and UI in the most efficient way. Automatic state binding and dependency tracking. Signals are effective in applications of any size.
Install:
npm install @preact/signals
Intro
Pain of state management in JavaScript is reacting to changes for a given value, because values are not directly observable. Solution is to store values in a variable and continuously check if they changed. Not ideal for performance. We want a way to express a value that tells us when it changes. That’s what Signals do.
A signal is an object with a .value property that holds a value. This has an important characteristic: a signal’s value can change, but the signal itself stays the same:
What does it mean?
In Preact, when a signal is passed down through a tree as props or context, we’re only passing around references to the signal. ???? The signal can be updated without re-rendering any components, since components see the signal and not its value. This lets us skip all of the expensive rendering work and jump immediately to any components in the tree that actually access the signal’s .value property.
So, I guess, it only re-render the components that depend on the signal. Other components that don’t reference the .value property of the signal will not re-render.
Signals have a second important characteristic, which is that they track when their value is accessed and when it is updated. In Preact, accessing a signal’s .value property from within a component automatically re-renders the component when that signal’s value changes.
In example above, we accessed count.value to retrieve the current value of count signal, however this is unnecessary. Instead, we can let Preact do all of the work for us by using the count signal directly in JSX:
Usage Example
Build a todo list app, where you can add and remove items in a todo list. Start by modeling the state. We’re going to need a signal that holds a list of todos first, which we can represent with an Array: