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'23const counterObservable = observe(0)45const App = () => {6 const [counter, setCounter] = useObserver(counterObservable)78 return (9 <button onClick={() => setCounter(counter + 1)}>10 Add +1 in {counter}11 </button>12 )13}