Quickstart
Elementos is a framework-agnostic, reactive state management library with an emphasis on composition. Elementos prefers to be explicit over concise, meaning you might write a little more code than you would with libraries like mobx, but you'll quickly understand the repercussions of changing any line of code and you won't feel like things are happening magically.
Installation
Observables
An observable is an object that can be observed using the observe
function and whose value is expected to change over time. Whenever the observable's value changes, all observers will be executed. There are three observable-producing functions packaged with elementos: atom
, molecule
, and derived
.
atom
An atom is an observable state container. We can get, set, and observe an atom's value.
tip
What's up with the $
at the end of count$
? This is known as Finnish Notation and is sometimes used to name variables that contain observable values. Elementos uses this notation where possible.
molecule
Molecules are used to aggregate observables. Observers of a molecule will be notified any time the dependencies of the molecule are updated.
derived
Derived observables are used to compute a new observable from an existing observable.
Actions
Atoms and molecules allow us to define custom actions, a powerful way to gatekeep our state.
Batching
Updates to atom state happen synchronously, as do the effects run by observers. As a result, the following code will run the observer callback twice during doubleIncrement
.
We can batch updates to ensure the observer callback runs only once after the completion of the batched update.
Composition
When we begin to compose all of these things together, we can create some really cool abstractions. Below is a state manager for dialogs that controls dialog visibility and allows for context data to be passed when opening a dialog.