React observing

useObserver

useObserver

Overview

The useObserverValue allows easy subscribing to an observable object. It automates all the manual process that would be needed.

The useObserver is very similar to the native useState react hook.

Definition

function useObserver<T>(observable: IObservable<T>): [T, TSetObservableState<T>]

Examples

Counter

1import { observe, useObserver } from 'react-observing'
2
3const counterObservable = observe(0)
4
5const App = () => {
6 const [counter, setCounter] = useObserver(counterObservable)
7
8 return (
9 <button onClick={() => setCounter(counter + 1)}>
10 Add +1 in {counter}
11 </button>
12 )
13}
Edit this page on GitHub