React observing
observe
observe
Overview
The observe
function is the most basic and most important function in the library.
It lets you build an object that can be used to register listeners for their value changes.
For the function we just need to input an initial value and it does the rest. The object returned by the function can be used in one of our hooks, see more here.
Definition
function observe<T>(initialValue: T): IObservable<T>
Examples
Simple values
...const NumberStore = observe(0)const BooleanStore = observe(true)const StringStore = observe('value')...
Objects
...// Object with observable propertiesconst Store = { prop1: observe('value'), prop2: observe('value'),}// Observable object with observable propertiesconst Store = observe({ prop1: observe('value'), prop2: observe('value'),})// Observable object with non observable propertiesconst Store = observe({ prop1: 'value', prop2: 'value',})...
Array
// Array with observable itemsconst Store = [ observe('value0'), observe('value1'), observe('value2'),]// Observable array with observable itemsconst Store = observe([ observe('value0'), observe('value1'), observe('value2'),])// Observable array with non observable itemsconst Store = observe(['value0', 'value1', 'value2',])