package.subscription.dist.index.d.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swr Show documentation
Show all versions of swr Show documentation
React Hooks library for remote data fetching
import { Key, SWRConfiguration, Middleware } from 'swr';
type SWRSubscription = (key: Key, { next }: {
next: (err?: Error | null, data?: Data) => void;
}) => () => void;
type SWRSubscriptionResponse = {
data?: Data;
error?: Error;
};
type SWRSubscriptionHook = (key: Key, subscribe: SWRSubscription, config?: SWRConfiguration) => SWRSubscriptionResponse;
declare const subscription: Middleware;
/**
* A hook to subscribe a SWR resource to an external data source for continuous updates.
* @experimental This API is experimental and might change in the future.
* @example
* ```jsx
* import useSWRSubscription from 'swr/subscription'
*
* const { data, error } = useSWRSubscription(key, (key, { next }) => {
* const unsubscribe = dataSource.subscribe(key, (err, data) => {
* next(err, data)
* })
* return unsubscribe
* })
* ```
*/
declare const useSWRSubscription: SWRSubscriptionHook;
export { SWRSubscription, SWRSubscriptionHook, SWRSubscriptionResponse, useSWRSubscription as default, subscription };