I use a react library ex. react-query
. This library provides a hook useQuery
ex.
const {data, isLoading} = useQuery(['users'], userService.getUsers);
My problem is that if I want to use two useQuery
hooks, in order to avoid having duplicated named variables:
data, isLoading
I have to do something like this:
const users = useQuery(['users'], userService.getUsers);
const products = useQuery(['products'], userService.getProducts);
...
// then use
users.data, users.isLoading
// and
products.data, products.isLoading
...
In order to make this logic more consistent, I tried to make my own custom hooks and rename the properties provided by useQuery
inside so I can access with spread syntax.
function useGetUsers(){
const {data, isLoading} = useQuery(['users'], userService.getUsers);
return {
usersData: data,
isUsersLoading: isLoading
}
}
and same with products.
So now, I can write the previous example as:
const {usersData, isUsersLoading} = useGetUsers();
const {productsData, isProductsLoading} = useGetProducts();
Of course, in order to have all the keys, I iterated over all the keys provided by useQuery
hook and customized my own custom keys with my own camel case rules, etc.
My question is, is this ok ? I mean, is this consider a good practice or just go with the users.isLoading
logic ??
const {data: usersData, isLoading: usersLoading} = useQuery(['users'], userService.getUsers);
andconst {data: productsData, isLoading: productsLoading} = useQuery(['products'], userService.getProducts);
– Coady