Title. Also, things I’ve tried:
-
My google search for the terms “react query with params” only gives me answers using the old, easier syntax:
useQuery(['product', prodId], getProduct)
… -
My google search for “react query v5 with params” gives me a bunch of half baked examples (here) using different notation
queryFn: ({ pageParam }) => fetchSomething(pageParam),
or:
queryKey: ['users', id],
queryFn: () => fetch(...),
which I think I’m too dumb to understand.
- ChatGPT gives me a more complete example:
type QueryContext = QueryFunctionContext<[string, string, string, string, number]>
const thisFetchFunctionGotTooComplicated = async ({ queryKey }: QueryContext) => {
const [, param1, param2, param3, param4] = queryKey
return (
await axios.get<MyInterface[]>('endpointname', { params: { param1, param2, param3, param4 } })
).data
}
const {
data,
error,
isLoading,
} = useQuery({
queryKey: ['uniquekey', myStringParam1, myStringParam2, myStringParam3, myNumberParam],
queryFn: ({queryKey}) => thisFetchFunctionGotTooComplicated(queryKey), // <-- tsc error: Argument of type '(string | number)[]' is not assignable to parameter of type '{ queryKey: [string, string, string, string, number];
})
which obviously doesn’t work.
So is there a simple example of using react-query v5 with query parameters in typescript?