T
TanStack2y ago
ambitious-aqua

Cache Invalidation not working on consecutive updates

import { useQuery } from '@tanstack/react-query';
import { QueryKeys } from 'queries/keys';
import { fetcher } from 'utils/fetcher';

function useGetHook({
token,
}: {
token: string;
}) {
return useQuery({
queryKey: [QueryKeys.KEY_VALUE],
queryFn: () =>
fetcher(
`someEndpoint`,
{ method: 'GET' },
token
),
});
}

export { useGetHook };

------------------------------------------------------------------

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { fetcher } from 'utils';
import { QueryKeys } from 'queries/keys';

export default function usePost({
token,
}: {
token: string;
}) {
const queryClient = useQueryClient();


return useMutation({
mutationFn: () => {

return fetcher(
`someEndpoint`,
{
method: 'POST',
body: {},
},
token
);
},
onError: () => {

},
onSuccess: (data) => {
queryClient.invalidateQueries({
queryKey: [QueryKeys.KEY_VALUE],
});

},
});
}
import { useQuery } from '@tanstack/react-query';
import { QueryKeys } from 'queries/keys';
import { fetcher } from 'utils/fetcher';

function useGetHook({
token,
}: {
token: string;
}) {
return useQuery({
queryKey: [QueryKeys.KEY_VALUE],
queryFn: () =>
fetcher(
`someEndpoint`,
{ method: 'GET' },
token
),
});
}

export { useGetHook };

------------------------------------------------------------------

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { fetcher } from 'utils';
import { QueryKeys } from 'queries/keys';

export default function usePost({
token,
}: {
token: string;
}) {
const queryClient = useQueryClient();


return useMutation({
mutationFn: () => {

return fetcher(
`someEndpoint`,
{
method: 'POST',
body: {},
},
token
);
},
onError: () => {

},
onSuccess: (data) => {
queryClient.invalidateQueries({
queryKey: [QueryKeys.KEY_VALUE],
});

},
});
}
Hey Guys, facing this issue where the cache invalidation is working well for the first time but when I send the post request again after second click it is not working, can someone help me with what I am doing wrong here The code above is the way I have structured my hooks
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?