0.0.22 • Published 5 years ago
istate v0.0.22
istate
A state management that is inspired on recoil
Simple state
import istate from 'istate';
const Count = istate(0);
const Increase = () => {
const [count, setCount] = Count();
setCount(count + 1);
};
Increase();
console.log(Count.get()); // 1
Modify state using reducer
const Increase = () => {
const [, setCount] = Count();
setCount((count) => count + 1);
};
Handle state changing
Count.subscribe(() => console.log('count state changed'));
State family
const Boxes = istate((id) => ({
id,
x: 0,
y: 0,
}));
const MoveBox = (id, offsetX, offsetY) => {
const [, setBox] = Boxes(id);
setBox((box) => ({
...box,
x: box.x + offsetX,
y: box.y + offsetY,
}));
};
const ResetBox = (id) => {
const [, boxApi] = Boxes(id);
boxApi.reset();
};
State dependencies
const Counter = istate(0);
const DoubleCounter = istate(() => {
const [counter] = Counter();
return counter * 2;
});
const Increase = () => {
const [counter, setCounter] = Counter();
setCounter(counter + 1);
};
Increase(); // Counter = 1, DoubleCounter = 2
Increase(); // Counter = 2, DoubleCounter = 4
0.0.22
5 years ago
0.0.20
5 years ago
0.0.19
5 years ago
0.0.18
5 years ago
0.0.17
5 years ago
0.0.16
5 years ago
0.0.15
5 years ago
0.0.14
5 years ago
0.0.13
5 years ago
0.0.12
5 years ago
0.0.11
5 years ago
0.0.10
5 years ago
0.0.9
5 years ago
0.0.8
5 years ago
0.0.3
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.7
5 years ago
0.0.6
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago