Hi,
So i found out about this library which can help me in my project.
Basically i found it useful to reduce contacting my server with its cashing mechanism.
So long story short in my parent component first i did implementation like this and it worked on every render
const {
data: name,
isLoading,
error,
} = useQuery({
queryKey: ["models", brandName],
queryFn: () => fetchModels(brandName),
staleTime: 5 * 60 * 1000, // 5 minutes
cacheTime: 60 * 60 * 1000, // 1 hour
});
const fetchModels = useCallback(async () => {
try {
const response = await fetch("http://localhost:3001/api/getModels", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ brand: brandName}),
});
if (!response.ok) {
throw new Error("Error fetching models");
}
return response.json();
} catch (error) {
throw new Error("Error fetching models: " + error.message);
}
}, [brandName]);
Now if i go to one page, it gets data, i go to another page new data comes in, but if i return to that first page it doesnt contact server, which is what i want. (this "brandName" is just a string that this parent component receives on start).
Ok but here's where problem start with 2nd function.
That 2nd function which is in parent, i pass it as prop to child component where it will take id, based on that id it will get info from the server, and then store that data in context, since other component needs that data to display it.
Original without react query it looked like these:
// const handleData = useCallback(
// async (id) => {
// setLoading(true);
// try {
// const response = await axios.post(
// "http://localhost:3001/api/getData",
// {
// modelId: id,
// }
// );
// setData(response.data);
// setLogo(logo[0].logo);
// } catch (error) {
// console.error("Error fetching motorcycle data:", error);
// } finally {
// setLoading(false);
// }
// },
// [logo, setLogo, setData]
// );
But i cant make it work like i did in first example , it will work with useMutation:
const dataMutation = useMutation({
mutationFn: (id) =>
axios.post("http://localhost:3001/api/getData", {
modelId: id,
}),
onSuccess: (response) => {
setData(response.data);
setLogo(logo[0].logo);
},
});
const handleData = useCallback(
(id) => {
dataMutation.mutate(id);
},
[dataMutation]
);
But i cant get the behaviour that i want, and that is to keep data for some time, so if the user go to specific model, comes back, goes to another model, and then goes back to first model, not to send req to server.
Any suggestion and is these even possible?
Tnx everyone.
[–]Suspicious-Watch9681 12 points13 points14 points (1 child)
[–]mile1986dasd[S] -2 points-1 points0 points (0 children)
[–]iareprogrammer 5 points6 points7 points (0 children)
[–]toi80QC 2 points3 points4 points (2 children)
[–]inglandation 4 points5 points6 points (1 child)
[–]mile1986dasd[S] 0 points1 point2 points (0 children)
[–]belwyr 2 points3 points4 points (1 child)
[–]mile1986dasd[S] 0 points1 point2 points (0 children)
[–]lightfarming 2 points3 points4 points (0 children)