admin管理员组文章数量:1425777
I am fetching all posts from json place holder api /posts and cache it as query cache key as ["posts"]
I want to use the cache posts to fetch by id
Right now I am trying
export const useGetPostById = (id: any) => {
const service = getQueryService();
const queryClient = useQueryClient();
const { data: post } = useQuery(["post", id], () => {
return service.getPostById(id);
});
return {
post,
};
};
and on my ponent trying to fetch this data by
const stupidData = queryClient.getQueryData(["posts", id]);
console.log(stupidData, "STUPID");
however here stupidData is undefined..
Here is how I'm fetching all posts
const { data: posts } = useQuery(["posts"], service.getPosts, {
enabled: true,
cacheTime: Infinity,
onSuccess: (data) => {
console.log("DATA", data);
data?.data.map((post: any) => {
queryClient.setQueryData(["posts", post.id], post);
});
},
});
I am fetching all posts from json place holder api /posts and cache it as query cache key as ["posts"]
I want to use the cache posts to fetch by id
Right now I am trying
export const useGetPostById = (id: any) => {
const service = getQueryService();
const queryClient = useQueryClient();
const { data: post } = useQuery(["post", id], () => {
return service.getPostById(id);
});
return {
post,
};
};
and on my ponent trying to fetch this data by
const stupidData = queryClient.getQueryData(["posts", id]);
console.log(stupidData, "STUPID");
however here stupidData is undefined..
Here is how I'm fetching all posts
const { data: posts } = useQuery(["posts"], service.getPosts, {
enabled: true,
cacheTime: Infinity,
onSuccess: (data) => {
console.log("DATA", data);
data?.data.map((post: any) => {
queryClient.setQueryData(["posts", post.id], post);
});
},
});
Share
Improve this question
edited Jul 27, 2022 at 8:09
Ahmet Firat Keler
4,1402 gold badges14 silver badges28 bronze badges
asked Jul 26, 2022 at 5:40
tryingToBeBettertryingToBeBetter
4251 gold badge7 silver badges25 bronze badges
2 Answers
Reset to default 2instead of accessing the queryData from queryClient.getQueryData()
can you use the defined hook in the ponent ?
At first, if the query hasnt been made yet, the data is supposed to be undefined. but since there is no state change, the ponent will not rerender to get the fetched query data.
export const useGetPostById = (id: number) => {
const service = getQueryService();
const queryClient = useQueryClient();
const { data: post } = useQuery(["posts", id], () => service.getPostById(id);
);
return {
post,
};
}
const Component = () => {
const id = 10;
const { post } = useGetPostById(id)
...
}
Addressing your last code example, you can delete the onSuccess function content. this is handeled automatically. The queryClient will save the ining data to the same key you are already requesting it with. In this case, its ["posts", id: number]
Edit: added extra information
Instead of just accessing the cache, I was just able to query the posts and find by id like
const myItem = posts?.data.find((item: any) => {
return item.id === Number(id);
});
本文标签: javascriptReact Query how to use cache data to fetch by idStack Overflow
版权声明:本文标题:javascript - React Query how to use cache data to fetch by id? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745377943a2656024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论