-
Hey everyone, I would like to create custom hooks with the ability to pass the query options. What I do have so far: const usePost = (id: number, options?: UseQueryOptions<Post>) =>
useQuery(["post", id], getPost, options) This is kind of working but the typing for the select function is wrong. const { data } = usePost(1234, { select: (post) => post.author }); Will lead to this error:
I don't know how to set up the options types to get the same result as useQuery. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
Try passing the result set type to useQuery
|
Beta Was this translation helpful? Give feedback.
-
Okay, I'm really bad at TypeScript but this is what I did and I think it is working: const usePost = <T = Post>(id: number, options?: UseQueryOptions<T, any, Post>) =>
useQuery(["post", id], getPost, options) And now if I use the custom hook with a select function the data type is correct. const { data } = usePost(1234, { select: (post) => post.author }); // 'data' is a string I just don't know why this works. I thought with this solution I always have to pass the type to the custom hook like this: |
Beta Was this translation helpful? Give feedback.
-
After allot of hit and trails finally I wrote this and it worked perfectly
|
Beta Was this translation helpful? Give feedback.
-
Hello this is my solution for useQuery and useMutation |
Beta Was this translation helpful? Give feedback.
Okay, I'm really bad at TypeScript but this is what I did and I think it is working:
And now if I use the custom hook with a select function the data type is correct.
I just don't know why this works. I thought with this solution I always have to pass the type to the custom hook like this:
usePost<string>(1234, { select: (post) => post.author });
, but this is not the case..