TODO: Publish to NPM and provide installation insructions
@nuka/state
is an application state-management library that focus on small
units of state instead of large chunks of global state. The idea is that small
state changes should not affect parts of a project that don't care/depend on
those changes. This is accomplished with the atom, the smallest unit of state
storage in @nuka/state
. You encapsulate your state values in an atom and then
you can use the value anywhere in your project. Combine that with subscribing
to the item to be notified when the value is modified and you can react to
state changes no matter when or where they occur.
Here is a simple counter example:
import { atom } from '@nuka/state';
// This is the magic, counter is now an atom and ready to be used.
const counter = atom(0);
const countDisplay = document.querySelector('#count-display');
// subscribe functions receive the atom as their argument allowing for flexible
// definition patterns
counter.subscribe(atom => {
countDisplay.textContent = `Current count: ${atom.value}`;
});
const incrementButton = document.querySelector('#increment-button');
// update receives the current value and should return the next new value,
// updates are queued and fired in order so multiple increments back to back
// will not cause any race conditions modifying the value.
incrementButton.addEventListener('click', () => counter.update(n => n + 1));
use.
a different value, such as taking two count atoms and providing their
sum as it's value).
atom updates.
APIs.
Contributions are totally welcome and greatly appreciated. In order to contribute please follow these simple steps:
nuka-state
and create a branch for your work.develop
branchBrandon Buck / @bbuck (Creator/maintainer)
Generated using TypeDoc