Skip to main content
Version: 5.0

useSubscription()

function useSubscription(
endpoint: ReadEndpoint,
params: object | null,
): void;

Great for keeping resources up-to-date with frequent changes.

When using the default polling subscriptions, frequency must be set in Endpoint, otherwise will have no effect.

Send null to params to unsubscribe.

Example

PriceResource.ts

import { Resource } from '@rest-hooks/rest';
import { EndpointExtraOptions } from '@rest-hooks/endpoint';

export default class PriceResource extends Resource {
readonly symbol: string | undefined = undefined;
readonly price: string = '0.0';
// ...

pk() {
return this.symbol;
}
static urlRoot = 'http://test.com/price/';

/** Used as default options for every Endpoint */
static getEndpointExtra(): EndpointExtraOptions {
return {
pollFrequency: 5000, // every 5 seconds
};
}
}

MasterPrice.tsx

import { useResource, useSubscription } from 'rest-hooks';
import PriceResource from 'resources/PriceResource';

function MasterPrice({ symbol }: { symbol: string }) {
const price = useResource(PriceResource.detail(), { symbol });
useSubscription(PriceResource.detail(), { symbol });
// ...
}

Only subscribe while element is visible

MasterPrice.tsx

import { useRef } from 'react';
import { useResource, useSubscription } from 'rest-hooks';
import PriceResource from 'resources/PriceResource';

function MasterPrice({ symbol }: { symbol: string }) {
const price = useResource(PriceResource.detail(), { symbol });
const ref = useRef();
const onScreen = useOnScreen(ref);
// null params means don't subscribe
useSubscription(PriceResource.detail(), onScreen ? null : { symbol });

return (
<div ref={ref}>{price.value.toLocaleString('en', { currency: 'USD' })}</div>
);
}

Using the last argument active we control whether the subscription is active or not based on whether the element rendered is visible on screen.

useOnScreen() uses IntersectionObserver, which is very performant.

Useful Endpoints to send

Resource provides these built-in:

  • detail()
  • list()

Be sure to extend these Endpoints with a pollFrequency to set the polling-rate.