package.dist.subscription.index.d.mts 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
The newest version!
import { MutatorCallback, Key, SWRConfiguration, Middleware } from 'swr/_internal';
type SWRSubscriptionOptions = {
next: (err?: Error | null, data?: Data | MutatorCallback) => void;
};
type SWRSubscription = SWRSubKey extends () => infer Arg | null | undefined | false ? (key: Arg, { next }: SWRSubscriptionOptions) => void : SWRSubKey extends null | undefined | false ? never : SWRSubKey extends infer Arg ? (key: Arg, { next }: SWRSubscriptionOptions) => void : never;
type SWRSubscriptionResponse = {
data?: Data;
error?: Error;
};
type SWRSubscriptionHook = (key: SWRSubKey, 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 { type SWRSubscription, type SWRSubscriptionHook, type SWRSubscriptionOptions, type SWRSubscriptionResponse, useSWRSubscription as default, subscription };