We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
See useRx and useRxTap here
useRx
useRxTap
import { useEffect, useState } from "react"; import { Observable, Subscription } from "rxjs"; import { tap } from 'rxjs/operators'; /** * Reactive Hook that returns a tuple of resolved value and error. * @param { Observable<T> } observable$ * @param { T } defaultValue */ export function useRx<T>( observable$: Observable<T>, defaultValue: T | (() => T) ): [T, any?] { const [x, setX] = useState(defaultValue); const [error, setError] = useState(); let subscription: Subscription; useEffect(() => { if (!subscription) { subscription = observable$.subscribe(setX, setError); } return () => subscription.unsubscribe(); }, [observable$]); return [x, error]; } /** * Reactive Hook that triggers callbacks on observable updates. * @param { Observable<T> } observable$ * @param { T } defaultValue */ export function useRxTap<T>( observable$: Observable<T>, next?: (x: T) => void, error?: (e: any) => void, complete?: () => void ): void { let subscription: Subscription; useEffect(() => { if (!subscription) { subscription = observable$.pipe(tap(next, error, complete)).subscribe(); } return () => subscription.unsubscribe(); }, [observable$]); }
Example usage:
export default function History({ last5$ }) { ... const [items, error] = useRx(last5$, []); return ( <ul> {items.map(item => ( <li key={item.id}>{item.id}</li> ))} </ul> ); }
Also related to #2
The text was updated successfully, but these errors were encountered:
The way to do error handling or tap is up to users, we don't plan to restrict them to the same way.
Sorry, something went wrong.
No branches or pull requests
See
useRx
anduseRxTap
hereExample usage:
Also related to #2
The text was updated successfully, but these errors were encountered: