Skip to content

Commit e569971

Browse files
committed
Add example for TypedUseQueryStateOptions
1 parent 129ba46 commit e569971

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,52 @@ export type UseQueryStateOptions<
436436
* This is particularly useful for setting default query behaviors such as
437437
* refetching strategies, which can be overridden as needed.
438438
*
439+
* @example
440+
* <caption>#### __Create a `useQuery` hook with default options__</caption>
441+
*
442+
* ```ts
443+
* import type {
444+
* SubscriptionOptions,
445+
* TypedUseQueryStateOptions,
446+
* } from '@reduxjs/toolkit/query/react'
447+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
448+
*
449+
* type Post = {
450+
* id: number
451+
* name: string
452+
* }
453+
*
454+
* const api = createApi({
455+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
456+
* tagTypes: ['Post'],
457+
* endpoints: (build) => ({
458+
* getPosts: build.query<Post[], void>({
459+
* query: () => 'posts',
460+
* }),
461+
* }),
462+
* })
463+
*
464+
* const { useGetPostsQuery } = api
465+
*
466+
* export const useGetPostsQueryWithDefaults = <
467+
* SelectedResult extends Record<string, any>,
468+
* >(
469+
* overrideOptions: TypedUseQueryStateOptions<
470+
* Post[],
471+
* void,
472+
* ReturnType<typeof fetchBaseQuery>,
473+
* SelectedResult
474+
* > &
475+
* SubscriptionOptions,
476+
* ) =>
477+
* useGetPostsQuery(undefined, {
478+
* // Insert default options here
479+
*
480+
* refetchOnMountOrArgChange: true,
481+
* refetchOnFocus: true,
482+
* ...overrideOptions,
483+
* })
484+
* ```
439485
*
440486
* @template ResultType - The type of the result `data` returned by the query.
441487
* @template QueryArg - The type of the argument passed into the query.

0 commit comments

Comments
 (0)