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 properties
const Store = {
prop1: observe('value'),
prop2: observe('value'),
}
// Observable object with observable properties
const Store = observe({
prop1: observe('value'),
prop2: observe('value'),
})
// Observable object with non observable properties
const Store = observe({
prop1: 'value',
prop2: 'value',
})
...

Array

// Array with observable items
const Store = [
observe('value0'),
observe('value1'),
observe('value2'),
]
// Observable array with observable items
const Store = observe([
observe('value0'),
observe('value1'),
observe('value2'),
])
// Observable array with non observable items
const Store = observe(['value0', 'value1', 'value2',])
Edit this page on GitHub