Skip to content

Instantly share code, notes, and snippets.

@ddoronin
Created January 8, 2023 04:14
Show Gist options
  • Save ddoronin/3beb02dd9710806ede045e501fdfce34 to your computer and use it in GitHub Desktop.
Save ddoronin/3beb02dd9710806ede045e501fdfce34 to your computer and use it in GitHub Desktop.
React hook for RxJS
export function use$<T>(subject$: BehaviorSubject<T>){
const [state, setState] = React.useState<T>(subject$.value);
React.useEffect(() => {
const subscription = subject$.subscribe({
next(data){
setState(data)
}
});
return () => {
subscription.unsubscribe();
}
}, [])
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment