0.6.1 • Published 4 months ago
yaasl v0.6.1
yaasl - yet another atomic store library
This project is meant for personal use only. I won't stop you from using it, but I would rather recommend to use a similar and more mature solution like jotai.
See the docs directory for detailed documentation.
Packages
Name | Description | |
---|---|---|
yaasl | Core package for vanilla JS. | demo |
yaasl/react | React bindings for yaasl . | demo |
Quickstart
- Install the package
$ npm i yaasl
- Create an atom
import { atom } from "yaasl";
const myAtom = atom({ defaultValue: 0 });
- Use the atom
// Read
const currentValue = myAtom.get(atom);
// Write
myAtom.set(nextValue);
// Subscribe to changes
myAtom.subscribe((value) => {
console.log(value);
});
Usage examples
Vanilla typescript
import { atom, CONFIG, middleware } from "yaasl";
// Provide an app name to yaasl
CONFIG.name = "demo-vanilla";
// Create a counter atom that is connected to the local storage
const counter = atom({
name: "counter", // local storage key will be "demo-vanilla/counter"
defaultValue: 0,
middleware: [localStorage()],
});
function setupCounter(element: HTMLButtonElement) {
const updateCounterText = (value: number) =>
(element.innerHTML = `count is ${value}`);
element.addEventListener("click", () => {
// Set the value of the atom
counter.set((previous) => previous + 1);
});
// Subscribe to value changes
counter.subscribe((value) => updateCounterText(value));
// Read the value of the atom in the store
updateCounterText(counter.get());
}
const counter = document.getElementById("counter");
setupCounter(counter);
React
import { atom, CONFIG, localStorage, useAtom } from "yaasl/react";
// Provide an app name to yaasl
CONFIG.name = "demo-react";
// Create a counter atom that is connected to the local storage
const counter = atom({
name: "counter", // local storage key will be "demo-vanilla/counter"
defaultValue: 0,
middleware: [localStorage()],
});
export const Counter = () => {
// Use the atom like you would use a state
const [value, setValue] = useAtom(counter);
const onClick = () => setValue((previous) => previous + 1);
return <button onClick={onClick}>count is {value}</button>;
};
0.6.1
4 months ago
0.6.0
4 months ago
0.5.0
7 months ago
0.4.0
8 months ago
0.3.0
8 months ago
0.2.0
8 months ago
0.1.0
10 months ago
0.1.0-alpha.5
10 months ago
0.1.0-alpha.4
11 months ago
0.1.0-alpha.3
11 months ago
0.1.0-alpha.2
11 months ago