The reactive system for the Leptos Web framework.
Fine-Grained Reactivity
Leptos is built on a fine-grained reactive system, which means that individual reactive values (“signals,” sometimes known as observables) trigger the code that reacts to them (“effects,” sometimes known as observers) to re-run. These two halves of the reactive system are inter-dependent. Without effects, signals can change within the reactive system but never be observed in a way that interacts with the outside world. Without signals, effects run once but never again, as there’s no observable value to subscribe to.
Here are the most commonly-used functions and types you'll need to build a reactive system:
Signals
- Signals:
create_signal, which returns a (ReadSignal,WriteSignal) tuple, orcreate_rw_signal, which returns a signalRwSignalwithout this read-write segregation. - Derived Signals: any function that relies on another signal.
- Memos: [
create_memo], which returns aMemo. - Resources: [
create_resource], which converts anasyncFutureinto a synchronousResourcesignal. - Triggers: [
create_trigger], creates a purely reactive [Trigger] primitive without any associated state.
Effects
- Use
create_effectwhen you need to synchronize the reactive system with something outside it (for example: logging to the console, writing to a file or local storage) - The Leptos DOM renderer wraps any [
Fn] in your template withcreate_effect, so components you write do not need explicit effects to synchronize with the DOM.
Example
use *;
// creates a new reactive Scope
// this is omitted from most of the examples in the docs
// you usually won't need to call it yourself
create_scope;