0.1.2 • Published 4 years ago
micro-reaction v0.1.2
one extremely simple observe state library, it's small, but strong.
Motivation
The project is inspired by observer-util and wana, but they is too large and not cleary read, so I refactor it. to be simple and better learn.
Install
npm install --save micro-reaction
Usage
Nested properties
import { observable, observe } from "micro-reaction";
const ob = observable({
a: {
b: 1
}
});
observe(() => console.log(ob.a.b));
// logs: 1
// logs: 2
ob.a.b = 2;
Dynamic properties
import { observable, observe } from "micro-reaction";
const ob = observable();
observe(() => console.log(ob.a));
// logs undefined
// logs 2
ob.a = 2;
Arrays
import { observable, observe } from "micro-reaction";
const ob = observable([]);
observe(() => console.log(ob));
// logs []
// logs [2]
ob.push(2);
API
observable(object)
create a observable object, return a proxy object with es6 proxy.
observe(function)
pass a function ,reaction auto run when it use a observable state.