Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "use-subscription"

Index

Functions

Functions

useSubscription

  • useSubscription<T>(stream$: Observable<T>, next?: function | null | undefined, error?: function | null | undefined, complete?: function | null | undefined): Subscription
  • Accepts an Observable and optional next, error, complete functions. These functions must be in correct order. Use undefined or null for placeholder.

    Subscription will unsubscribe when unmount, you can also unsubscribe manually.

    Note that changes of callbacks will not trigger an emission. If you need that just create another Observable of the callback with useObservable.

    (From v2.0) You can access closure directly inside callback like in useEffect. useSubscription will ensure the latest callback is called.

    Examples:

    const subscription = useSubscription(events$, e => console.log(e.type))

    On complete

    const subscription = useSubscription(events$, null, null, () => console.log('complete'))

    Access closure:

    const [debug, setDebug] = useState(false)
    const subscription = useSubscription(events$, null, error => {
      if (debug) {
        console.log(error)
      }
    })

    Invoke props callback

    const subscription = useSubscription(events$, props.onEvent)

    Type parameters

    • T

    Parameters

    • stream$: Observable<T>
    • Optional next: function | null | undefined
    • Optional error: function | null | undefined
    • Optional complete: function | null | undefined

    Returns Subscription

Generated using TypeDoc