Last active
February 9, 2024 21:24
-
-
Save mkarajohn/1309612398d81ce8f44acf711ba21e61 to your computer and use it in GitHub Desktop.
Customizable specific react-query hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usecase: Say you have an endpoint `/clients` and you have created a custom | |
// hook, that wraps over a `useQuery` that has a fixed `queryKey` and `queryFn` | |
// (because it is supposed to only fetch data from the `/clients` endpoint) | |
// However you want to be able to pass a valid `useQuery` config object in order | |
// to customize its behaviour (except for passing a custom queryKey or queryFn) | |
// This is one way to do this. | |
// 2023 Dimitris Karagiannis | |
// Twitter: @MitchKarajohn | |
export function useSpecificButCustomizableQuery( | |
queryOptions: Omit< | |
UseQueryOptions< | |
TypeOfReturnValue, | |
unknown, | |
TypeOfReturnValue, | |
TypeOfTheQueryKey | |
>, | |
'queryKey' | 'queryFn' | |
> = {} | |
) { | |
return useQuery< | |
TypeOfReturnValue, | |
unknown, | |
TypeOfReturnValue, | |
TypeOfTheQueryKey | |
>({ | |
...queryOptions, | |
queryKey: ['your', 'key'], | |
queryFn: (): Promise<TypeOfReturnValue> => {} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment