React query doesn't sustain its cache after queryClient.setQueryData, also it's not working on defaultOptions like under. i am using react query developer tool and it show that that data get stored after API call but i saw a property gcTimeout: 23. i think this is effecting and have tried every possible solution available on internet.
// defined in app.tsx
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
cacheTime: 100000, // 10 min
},
},
});
<QueryClientProvider client={queryClient}>
let me share the custom hook i created for mutation. and how i am calling it.
// defined in hooks/useFetch.ts
const useFetch = (requests: Request[]) => {
const queryClient = useQueryClient();
const fetchDataAndMutate = () => {
return requests.map((request) => {
const {
key,
api,
save = true,
} = request;
const {mutate, isLoading} = useMutation({
mutationFn: async (payload) => postUtil(api, payload),
onMutate: (variables) => {
return {id: 1};
},
onError: (error, variables, context) => {
// An error happened!
console.log(`Error: ${error}`);
},
onSuccess: (response, variables, context) => {
if (response.status) {
if (response.status === 200) {
//success
if (save) queryClient.setQueryData([key], response.data.data);
}
} else if (response.response.status) {
console.log(
"response.response.status",
response.response.data.Message
);
}
},
});
return {
mutate,
isLoading,
};
});
};
const mutates = fetchDataAndMutate();
useEffect(() => {
// to hit api as it is defined
requests.forEach((request, index) => {
if (request?.init)
if (typeof mutates[index].mutate === "function")
mutates[index].mutate();
});
}, []);
return mutates;
};
used the hooks as following ->
// defined in containers/analysis
const apiConfig = [
{
key: ALL_MAPPING_AREAS,
api: getAllMappingAreasApi,
init: true,
},
{
key: MAPPING_AREA_DETAIL,
api: getMappingAreaDetailApi,
},
];
const mutates = useFetch(apiConfig);
const [
getAllMappingAreas,
getBoundedAreaDetail,
] = mutates;
const allMappingAreas = queryClient.getQueryData([ALL_MAPPING_AREAS]);
const mappingAreaDetail = queryClient.getQueryData([MAPPING_AREA_DETAIL]);
// i am receiving data in allMappingAreas but it vanish fater almost 5min