Created
January 8, 2023 04:14
-
-
Save ddoronin/3beb02dd9710806ede045e501fdfce34 to your computer and use it in GitHub Desktop.
React hook for RxJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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