2.1.1 • Published 5 months ago
simple-reactor v2.1.1
Super Simple Reactive Variables
I was unable to find my own verison of this. Something simple and easy to use. Hence why I've created my own!
Usage
Import the library.
import { observeable } from 'simple-reactor'
or import anyname from 'simple-reactor'
JSDoc's are provided. Alongside typescript definitions.
Create a new observable with any value you want:
const obs = new observable(1);
Subscribe to it to know when the value changes:
obs.subscribe((value, unsub) => {
console.log('Value: ', value);
unsub();
})
The second paramater is a function used to unsubscribe from the object. It can be safely ommited if now wanting to be used.
And change it like so:
obs.set(2);
If you want to use the value, just use get:
obs.get();
Full Code
import { observable } from 'simple-reactor';
const obs = new observable(1);
obs.subscribe((value, unsub) => {
console.log('value', value);
if (value == 3) ubsub();
})
obs.set(2);
obs.set(3);