React hook that safely manages subscriptions in concurrent mode.

This utility can be used for subscriptions to a single value that are typically only read in one place and may update frequently (e.g. a component that subscribes to a geolocation API to show a dot on a map).

When should you NOT use this?

Most other cases have better long-term solutions:

Redux/Flux stores should use the context API instead.

I/O subscriptions (e.g. notifications) that update infrequently should use a mechanism like react-cache instead.

instead. Complex libraries like Relay/Apollo should manage subscriptions manually with the same techniques which this library uses under the hood (as referenced here) in a way that is most optimized for their library usage.

Limitations in concurrent mode

use-subscription is safe to use in concurrent mode. However, it achieves correctness by sometimes de-opting to synchronous mode, obviating the benefits of concurrent rendering. This is an inherent limitation of storing state outside of React's managed state queue and rendering in response to a change event.

The effect of de-opting to sync mode is that the main thread may periodically be blocked (in the case of CPU-bound work), and placeholders may appear earlier than desired (in the case of IO-bound work).

For full compatibility with concurrent rendering, including both time-slicing and React Suspense, the suggested longer-term solution is to move to one of the patterns described in the previous section.

What types of subscriptions can this support?

This abstraction can handle a variety of subscription types, including:

Event dispatchers like HTMLInputElement .

. Custom pub/sub components like Relay's FragmentSpecResolver .

. Observable types like RxJS BehaviorSubject and ReplaySubject . (Types like RxJS Subject or Observable are not supported, because they provide no way to read the "current" value after it has been emitted.)

Note that JavaScript promises are also not supported because they provide no way to synchronously read the "current" value.

Installation

yarn add use-subscription npm install use-subscription

Usage

To configure a subscription, you must provide two methods: getCurrentValue and subscribe .

In order to avoid removing and re-adding subscriptions each time this hook is called, the parameters passed to this hook should be memoized. This can be done by wrapping the entire subscription with useMemo() , or by wrapping the individual callbacks with useCallback() .

Subscribing to event dispatchers

Below is an example showing how use-subscription can be used to subscribe to event dispatchers such as DOM elements.

import React , { useMemo } from " react " ; import { useSubscription } from " use-subscription " ; function Example ( { input } ) { const subscription = useMemo ( ( ) => ( { getCurrentValue : ( ) => input . value , subscribe : callback => { input . addEventListener ( " change " , callback ) ; return ( ) => input . removeEventListener ( " change " , callback ) ; } } ) , [ input ] ) ; const value = useSubscription ( subscription ) ; }

Subscribing to observables

Below are examples showing how use-subscription can be used to subscribe to certain types of observables (e.g. RxJS BehaviorSubject and ReplaySubject ).

Note that it is not possible to support all observable types (e.g. RxJS Subject or Observable ) because some provide no way to read the "current" value after it has been emitted.

BehaviorSubject

const subscription = useMemo ( ( ) => ( { getCurrentValue : ( ) => behaviorSubject . getValue ( ) , subscribe : callback => { const subscription = behaviorSubject . subscribe ( callback ) ; return ( ) => subscription . unsubscribe ( ) ; } } ) , [ behaviorSubject ] ) ; const value = useSubscription ( subscription ) ;

ReplaySubject