How to handle param build by two or more hooks?
Right now I have an a opportunity to rebuild one of my company applications and i want to use react-query instead of useeffect's, but I have one problem with that.
Main useEffect that fetch records data have many query paramas like
&ordering=${sortNameColumnAsc} and that's something I can handle, but I don't really have anything in mind, how to handle something like that &number_calls=${conPerLeadStart},${conPerLeadEnd}.
And in that app we have many of params that're build on two hooks.
How can I handle that using react-query?

5 Replies
optimistic-gold•3y ago
all params go to the query key.
equal-aquaOP•3y ago
So how will this work? Still cant figure it out
Right now I have something like this:
const itemListQuery = useQuery(['data', campaign, authToken], () => getData(campaign, authToken));
So if I want to for example add param 'Number_Calls' ( just like in a picture i send before) I just have to add -> useQuery(['data', campaign, authToken, conPerLeadStart, conPerLeadEnd] ?
How will it know that it should make url like '.../&number_calls={x},{y} ?
optimistic-gold•3y ago
that's right.
it doesn' t make the url for you - you have to build the url
you can either use closures like you did above:
or use the
queryKey passed into the queryFn:
other-emerald•3y ago
We have wrapped the
fetch function (and use it as default with react-query), and there we assume all query keys are equal to the path segments of the request. And if there's an object in the key, it will be appended as a query string.
Some code we use:
equal-aquaOP•3y ago
Ooo, that's how it works! So basically I pass all paramas to useQuery, so -> useQuery(['data', campaign, authToken, X, Y, Z], () => getData(campaign, authToken, X, Y, Z));
and then in my 'getData' function with axios I simply pass params (params : {
1: X,
2: Y,Z
} to my url