Open History to see the last commit
1---
2id: default-query-function
3title: Default Query Function
4ref: docs/framework/react/guides/default-query-function.md
5---
6β
7[//]: # 'Example'
8β
9```tsx
10// Define a default query function that will receive the query key
11const defaultQueryFn = async ({ queryKey }) => {
12 const { data } = await axios.get(
13 `https://jsonplaceholder.typicode.com${queryKey[0]}`,
14 )
15 return data
16}
17β
18// provide the default query function to your app with defaultOptions
19const queryClient = new QueryClient({
20 defaultOptions: {
21 queries: {
22 queryFn: defaultQueryFn,
23 },
24 },
25})
26β
27function App() {
28 return (
29 <QueryClientProvider client={queryClient}>
30 <YourApp />
31 </QueryClientProvider>
32 )
33}
34β
35// All you have to do now is pass a key!
36function Posts() {
37 const postsQuery = useQuery(() => ({ queryKey: ['/posts'] }))
38β
39 // ...
40}
41β
42// You can even leave out the queryFn and just go straight into options
43function Post(props) {
44 const postQuery = useQuery(() => ({
45 queryKey: [`/posts/${props.postId}`],
46 enabled: !!props.postId,
47 }))
48β
49 // ...
50}
51```
52β
53[//]: # 'Example'