Is it possible to update data after mutation without using useQuery hook higher up in the tree? #2379
-
Hello I know that the title might be confusing/misleading, so I'll try to explain. I have a simple component that gets an array of data from the server, maps to post components and each of them can be liked/disliked by users. My question is, can I do something like: https://codesandbox.io/s/hardcore-morning-jgi4h?file=/components/PostContainer.js function Home({
posts,
}:
InferGetServerSidePropsType<typeof getServerSideProps>) {
return (
<>
<div
css={{
display: "flex",
flexGrow: 1,
justifyContent: "space-between",
position: "relative",
alignItems: "flex-start",
[mQ("1000")]: { flexDirection: "column", alignItems: "center" },
}}
>
<>
<Feed posts={posts} />
</>
</div>
</>
);
}
export const getServerSideProps: GetServerSideProps = async (context) => {
const queryClient = new QueryClient();
await queryClient.prefetchQuery("posts", async () => {
const postsResponse = await fetch("http://localhost:3000/api/posts", {
method: "GET",
});
return await postsResponse.json();
});
const withKeys: any = [];
dehydrate(queryClient).queries.forEach(
(query) =>
(withKeys[query.queryKey as any] = (query.state.data as any).data)
);
return {
props: {
posts: withKeys["posts"],
// sending data directly instead of dehydratedState: dehydrate(queryClient),
users: users.data,
},
};
}; The data is available and all that, but when using useMutation with onSuccess hook the data doesnt get updated, only after refresh/location change. const mutation = useMutation(() => likePost(post._id), {
onSuccess: () => {
queryClient.invalidateQueries("posts");
},
}); I'm assuming its because we only prefetched query in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Are you hydrating the queryClient on the frontend? |
Beta Was this translation helpful? Give feedback.
Are you hydrating the queryClient on the frontend?