0.0.22 • Published 4 years ago

istate v0.0.22

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

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

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago