use-subscription
Reusable hooks
Last updated 19 days ago by gaearon .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install use-subscription 
SYNC missed versions from official npm registry.

use-subscription

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.
  • 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
yarn add use-subscription

# NPM
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";

// In this example, "input" is an event dispatcher (e.g. an HTMLInputElement)
// but it could be anything that emits an event and has a readable current value.
function Example({ input }) {

  // Memoize to avoid removing and re-adding subscriptions each time this hook is called.
  const subscription = useMemo(
    () => ({
      getCurrentValue: () => input.value,
      subscribe: callback => {
        input.addEventListener("change", callback);
        return () => input.removeEventListener("change", callback);
      }
    }),

    // Re-subscribe any time our input changes
    // (e.g. we get a new HTMLInputElement prop to subscribe to)
    [input]
  );

  // The value returned by this hook reflects the input's current value.
  // Our component will automatically be re-rendered when that value changes.
  const value = useSubscription(subscription);

  // Your rendered output goes here ...
}

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();
    }
  }),

  // Re-subscribe any time the behaviorSubject changes
  [behaviorSubject]
);

const value = useSubscription(subscription);

ReplaySubject

const subscription = useMemo(
  () => ({
    getCurrentValue: () => {
      let currentValue;
      // ReplaySubject does not have a sync data getter,
      // So we need to temporarily subscribe to retrieve the most recent value.
      replaySubject
        .subscribe(value => {
          currentValue = value;
        })
        .unsubscribe();
      return currentValue;
    },
    subscribe: callback => {
      const subscription = replaySubject.subscribe(callback);
      return () => subscription.unsubscribe();
    }
  }),

  // Re-subscribe any time the replaySubject changes
  [replaySubject]
);

const value = useSubscription(subscription);

Current Tags

  • 0.0.0-experimental-4ead6b530                                ...           experimental (2 months ago)
  • 1.5.1                                ...           latest (19 days ago)
  • 0.0.0-8e5adfbd7                                ...           next (a month ago)

78 Versions

  • 1.5.1                                ...           19 days ago
  • 0.0.0-8e5adfbd7                                ...           a month ago
  • 1.5.0                                ...           a month ago
  • 0.0.0-experimental-4ead6b530                                ...           2 months ago
  • 1.5.0-rc.3                                ...           2 months ago
  • 0.0.0-4ead6b530                                ...           2 months ago
  • 1.5.0-rc.2                                ...           2 months ago
  • 0.0.0-3834174dd                                ...           2 months ago
  • 1.5.0-rc.1                                ...           3 months ago
  • 0.0.0-4e6999103                                ...           3 months ago
  • 1.5.0-rc.0                                ...           4 months ago
  • 0.0.0-experimental-94c0244ba                                ...           4 months ago
  • 0.0.0-94c0244ba                                ...           4 months ago
  • 0.0.0-experimental-f77c7b9d7                                ...           4 months ago
  • 0.0.0-f77c7b9d7                                ...           4 months ago
  • 0.0.0-experimental-3d0895557                                ...           4 months ago
  • 0.0.0-3d0895557                                ...           4 months ago
  • 0.0.0-experimental-ede917064                                ...           4 months ago
  • 0.0.0-experimental-4c8c98ab9                                ...           4 months ago
  • 0.0.0-experimental-7f28234f8                                ...           5 months ago
  • 0.0.0-7f28234f8                                ...           5 months ago
  • 0.0.0-experimental-33c3af284                                ...           7 months ago
  • 0.0.0-33c3af284                                ...           7 months ago
  • 0.0.0-experimental-e5d06e34b                                ...           8 months ago
  • 0.0.0-experimental-d7382b6c4                                ...           8 months ago
  • 0.0.0-d7382b6c4                                ...           8 months ago
  • 1.4.1                                ...           8 months ago
  • 0.0.0-da834083c                                ...           8 months ago
  • 0.0.0-experimental-aae83a4b9                                ...           8 months ago
  • 0.0.0-experimental-8b155d261                                ...           9 months ago
  • 0.0.0-235a6c4af                                ...           9 months ago
  • 0.0.0-experimental-235a6c4af                                ...           9 months ago
  • 0.0.0-experimental-355970aa4                                ...           9 months ago
  • 1.4.0                                ...           9 months ago
  • 0.0.0-experimental-d28bd2994                                ...           9 months ago
  • 0.0.0-d28bd2994                                ...           9 months ago
  • 0.0.0-experimental-8d7535e54                                ...           9 months ago
  • 0.0.0-8d7535e54                                ...           9 months ago
  • 0.0.0-5de5b6150                                ...           9 months ago
  • 0.0.0-experimental-5de5b6150                                ...           9 months ago
  • 0.0.0-ea6ed3dbb                                ...           9 months ago
  • 0.0.0-experimental-ea6ed3dbb                                ...           9 months ago
  • 0.0.0-experimental-241c4467e                                ...           10 months ago
  • 0.0.0-241c4467e                                ...           10 months ago
  • 0.0.0-experimental-57333ca33                                ...           10 months ago
  • 0.0.0-57333ca33                                ...           10 months ago
  • 0.0.0-experimental-f42431abe                                ...           a year ago
  • 0.0.0-f42431abe                                ...           a year ago
  • 0.0.0-experimental-2c169a568                                ...           a year ago
  • 0.0.0-experimental-3f3fe0874                                ...           a year ago
  • 0.0.0-experimental-0e6839614                                ...           a year ago
  • 1.3.0                                ...           a year ago
  • 0.0.0-experimental-b53ea6ca0                                ...           a year ago
  • 0.0.0-b53ea6ca0                                ...           a year ago
  • 0.0.0-38dd17ab9                                ...           a year ago
  • 0.0.0-experimental-38dd17ab9                                ...           a year ago
  • 0.0.0-f6b8d31a7                                ...           a year ago
  • 0.0.0-experimental-f6b8d31a7                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 0.0.0-experimental-5faf377df                                ...           a year ago
  • 0.0.0-5faf377df                                ...           a year ago
  • 0.0.0-experimental-1022ee0ec                                ...           a year ago
  • 0.0.0-1022ee0ec                                ...           a year ago
  • 0.0.0-experimental-7082d5a2d                                ...           a year ago
  • 0.0.0-7082d5a2d                                ...           a year ago
  • 0.0.0-experimental-d7feeb25a                                ...           a year ago
  • 0.0.0-experimental-d364d8555                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 0.0.0-4ab6305f6                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 0.0.0-d346d9208                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 0.0.0-3694a3b5e                                ...           a year ago
  • 0.0.0-b4b8a349a                                ...           a year ago
  • 0.0.0-45b6443c9                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.0.0-8d5403877                                ...           a year ago
  • 0.0.0                                ...           2 years ago

Copyright 2014 - 2016 © taobao.org |