vue-redux-hooks
Version:
Redux hooks for Vue
52 lines (51 loc) • 3.17 kB
TypeScript
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState';
import type { QueryActionCreatorResult } from '@reduxjs/toolkit/dist/query/core/buildInitiate';
import type { QueryArgFrom, QueryDefinition } from '@reduxjs/toolkit/dist/query/endpointDefinitions';
import { SkipToken } from '@reduxjs/toolkit/query';
import { Reactive, ReactiveRecord } from './util';
export interface UseQuerySubscriptionOptions extends SubscriptionOptions {
/**
* Prevents a query from automatically running.
*
* @remarks
* When `skip` is true (or `skipToken` is passed in as `arg`):
*
* - **If the query has cached data:**
* * The cached data **will not be used** on the initial load, and will ignore updates from any identical query until the `skip` condition is removed
* * The query will have a status of `uninitialized`
* * If `skip: false` is set after skipping the initial load, the cached result will be used
* - **If the query does not have cached data:**
* * The query will have a status of `uninitialized`
* * The query will not exist in the state when viewed with the dev tools
* * The query will not automatically fetch on mount
* * The query will not automatically run when additional components with the same query are added that do run
*
* @example
* ```tsx
* // codeblock-meta title="Skip example"
* const Pokemon = ({ name, skip }: { name: string; skip: boolean }) => {
* const { data, error, status } = useGetPokemonByNameQuery(name, {
* skip,
* });
*
* return (
* <div>
* {name} - {status}
* </div>
* );
* };
* ```
*/
skip?: boolean;
/**
* Defaults to `false`. This setting allows you to control whether if a cached result is already available, RTK Query will only serve a cached result, or if it should `refetch` when set to `true` or if an adequate amount of time has passed since the last successful query result.
* - `false` - Will not cause a query to be performed _unless_ it does not exist yet.
* - `true` - Will always refetch when a new subscriber to a query is added. Behaves the same as calling the `refetch` callback or passing `forceRefetch: true` in the action creator.
* - `number` - **Value is in seconds**. If a number is provided and there is an existing query in the cache, it will compare the current time vs the last fulfilled timestamp, and only refetch if enough time has elapsed.
*
* If you specify this option alongside `skip: true`, this **will not be evaluated** until `skip` is false.
*/
refetchOnMountOrArgChange?: boolean | number;
}
export declare type UseQuerySubscription<D extends QueryDefinition<any, any, any, any>> = (arg: Reactive<QueryArgFrom<D> | SkipToken>, options?: ReactiveRecord<UseQuerySubscriptionOptions>) => Pick<QueryActionCreatorResult<D>, 'refetch'>;
export declare const createUseQuerySubscription: <D extends QueryDefinition<any, any, any, any, string>>(endpoint: any) => UseQuerySubscription<D>;