Skip to content

Commit

Permalink
Add example for TypedUseQueryStateOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Sep 11, 2024
1 parent e89f4b0 commit 6610d56
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,52 @@ export type UseQueryStateOptions<
* This is particularly useful for setting default query behaviors such as
* refetching strategies, which can be overridden as needed.
*
* @example
* <caption>#### __Create a `useQuery` hook with default options__</caption>
*
* ```ts
* import type {
* SubscriptionOptions,
* TypedUseQueryStateOptions,
* } from '@reduxjs/toolkit/query/react'
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
*
* type Post = {
* id: number
* name: string
* }
*
* const api = createApi({
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
* tagTypes: ['Post'],
* endpoints: (build) => ({
* getPosts: build.query<Post[], void>({
* query: () => 'posts',
* }),
* }),
* })
*
* const { useGetPostsQuery } = api
*
* export const useGetPostsQueryWithDefaults = <
* SelectedResult extends Record<string, any>,
* >(
* overrideOptions: TypedUseQueryStateOptions<
* Post[],
* void,
* ReturnType<typeof fetchBaseQuery>,
* SelectedResult
* > &
* SubscriptionOptions,
* ) =>
* useGetPostsQuery(undefined, {
* // Insert default options here
*
* refetchOnMountOrArgChange: true,
* refetchOnFocus: true,
* ...overrideOptions,
* })
* ```
*
* @template ResultType - The type of the result `data` returned by the query.
* @template QueryArg - The type of the argument passed into the query.
Expand Down

0 comments on commit 6610d56

Please sign in to comment.