@iniz/core
v0.8.0
Published
Iniz is a reactive state library. Try it out on our [website](https://iniz.netlify.app)!
Downloads
11
Readme
Iniz
Iniz is a reactive state library. Try it out on our website!
npm i @iniz/core
Guide
Getting started
Create an atom
Atom is the smallets unit of reactive state.
import { atom } from "@iniz/core";
const timer$ = atom(new Date());
// It can be a complex nested object/array as well...
const nestedCounter$ = atom({
obj: {
array: [{ count: 3 }],
message: "Hello World",
},
});
Mutate the atom value
Call the atom to read/write it.
timer$(); // Returns latest value e.g. `2019-08-31T00:00:00.000Z`
setInterval(() => {
nestedCounter$().obj.array[0].count++;
timer$(new Date());
// Calling it as function also sets `value`
}, 1000);
// Later on...
timer$(); // Returns latest value e.g. `2022-08-31T00:00:00.000Z`
nestedCouner$().obj.array[0].count;
Subscribe to atom
Use effect()
to subscribe to value change.
const dispose = effect(() => {
console.log("Updated timer: ", timer$());
});
// Execute `dispose` to stop effect
dispose();
Use computed()
to get calculated value from multiple atoms.
const timerAndCounter$ = computed(
() => `Computed: '${nestedCounter$().obj.array[0]}' '${timer$()}'`
);
timerAndCounter$(); // Returns "Computed: 2022-08-31T00:00:00.000Z 4"