React observing

useObserverValue

useObserverValue

Overview

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

The useObserverValue is very similar to the useObserver hook. However, it only returns the state and not a function to change that state.

useObserver vs useObserverValue

The useObserver hook by default returns a state and a function to change it. When you only want the state value and are not interested in changing it, it is recommended to use this method. It is simpler and makes the application more optimized.

Definition

function useObserverValue<T>(observable: IObservable<T>): T

Examples

Counter

1import { observe, useObserverValue } from 'react-observing'
2
3const counterObservable = observe(10)
4
5const App = () => {
6 const counter = useObserverValue(counterObservable)
7
8 return (
9 <p>Counter is in {counter}</p>
10 )
11}
Edit this page on GitHub