-
I believe that the options for 'useQuery' should be defined inside the custom hook rather than being passed as arguments, and that for mutations, 'invalidateQueries' should be set inside the custom hook to execute onSuccess. However, I have seen cases where these are managed externally. Managing it this way seems like it would violate the Dependency Inversion Principle and could lead to inconsistent behavior, making debugging and maintenance difficult as the number of usages increases and each has different options. What do you think about this? Also, could you tell me the officially recommended usage by Tanstack Query? Isn't this approach incorrect? function usePosts(option?: UseQueryOptions) {
return useQuery({
queryKey: ['posts'],
queryFn: async (): Promise<Array<Post>> => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
return await response.json()
},
...option
})
}
const { data, error, isLoading } = usePosts({
staleTime: 1000 * 60 * 5,
}); Shouldn't we implement it in the form provided in the official documentation? function usePosts() {
return useQuery({
queryKey: ['posts'],
queryFn: async (): Promise<Array<Post>> => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
return await response.json()
},
staleTime: 1000 * 60 * 10,
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
https://tkdodo.eu/blog/the-query-options-api