React observing

useSetObserver

useSetObserver

Overview

O useSetObserver retorna apenas um metodo que pode alterar o valor do observable.

Using this hook is recommended to optimize your application's performance. Since the returned method has no state effects. This way, your component won't have a new render stream every time the set is executed.

useObserver vs useSetObserver

useObserver always returns a state and a method for changing the state. Whenever the state change happens, a new rendering stream happens, this flow can be avoided using this hook.

Definition

function useSetObserver<T>(observable: IObservable<T>): TSetObservableState<T>

Examples

Counter

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