-
Hi there, Here is my use case:
I read carefully the discussions #351 #1549 and understand that because React Query is declarative you cannot pass any parameter to refetch. so something like this doesn't exist: React Query custom hook import { UseQueryOptions, useQuery } from 'react-query';
import { api } from '@services/api';
export const useQueryHook = (param1: string, param2: string, options?: UseQueryOptions): any => {
const queryFn = () => {
return api.get(`/endpoint`, {
params: {
param1,
param2,
},
});
};
const query = useQuery(['myQuery', param1, param2], queryFn, {
onSuccess: () => console.info('Success'),
onError: () => console.error('Error'),
...options,
});
return query;
};
export default useQueryHook; Custom hook that call React query custom hook import { useState, useEffect } from "react";
import { useQueryHook } from "../api/useQueryHook";
export const useBusinessLogic = (): any => {
const [param1, setParam1] = useState("");
const [param2, setParam2] = useState("");
const { data } = useQueryHook(param1, param2, {
enabled: param1 !== "" && param2 !== "",
});
const onFormSubmit = (values: any) => {
setParam1(values.param1);
setParam2(values.param2);
};
useEffect(() => {
if (data) {
console.log(data);
}
}, [data]);
return {
onFormSubmit
};
};
export default useBusinessLogic; Here are my questions:
Thanks a lot ! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
It really depends on what you want to do. Is your request executing side-effects on the backend, like logging in a user? Then it's most certainly a mutation. Be aware that mutations really only run once when you invoke them. Queries on the other hand can re-fetch for a variety of reasons after you enable them. So for example, a window focus event could trigger a refetch. If it's truly a GET request, it doesn't matter because they are idempotent. Also, ask yourself if you want to share data across components, because useMutation cannot do that out of the box.
It's definitely a good implementation of useLazyQuery - except that you don't need the useEffect, it doesn't do anything react-query related?
A query that you permanently disable and then call |
Beta Was this translation helpful? Give feedback.
-
Makes perfect sense, thanks a lot for your detailed answer :-)
Okay so in this case after this form is submitted we will never refetch it with the same parameter (the form disappears after the data comes back and its value get reset). Can you confirm in this case that:
Thanks again, |
Beta Was this translation helpful? Give feedback.
-
Sure, I am working on a multi-steps dialog where at step 1 you have this form that make the GET request then when the data comes back this form disappear and the data are used in step 2 to make others calls. |
Beta Was this translation helpful? Give feedback.
It really depends on what you want to do. Is your request executing side-effects on the backend, like logging in a user? Then it's most certainly a mutation. Be aware that mutations really only run once when you invoke them. Queries on the other hand can re-fetch for a variety of reasons after you enable them. So for example, a window focus event could trigger a refetch. If it's truly a GET request, it doesn't matter because they are idempotent.
Also, ask yourself if you want to share data across components, because useMutation cannot do that out of the box.