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 component 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);
});
},
});