What is the best (or alternate) ways to fetch the server data from a child component when the parent component hits an api that returns all the data that we need? #1619
-
Should I be using useQuery for all children components or queryClient.getQueryData? My fear is if i use useQuery in the children component I may be potentially adding an unnecessary additional request, but if I'm wrong then I'm adding complexity for no reason. For example the layout is like this:
What I've done is put the useQuery at the and then for any children components, I use the queryClient.getQueryData. I've created a few wrapper functions so I have a consistent way to grab the correct key based on url path. useQueryCurrentGridViewlModel - is basically useQuery with the key already returned
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 16 replies
-
what I don't like about
there are a couple of approaches:
now Child1A will only re-render by itself if |
Beta Was this translation helpful? Give feedback.
-
Here is export function useQueryObserver(queryKey, options) {
const queryClient = useQueryClient();
const [result, setResult] = React.useState(undefined);
const observer = React.useMemo(
() => new QueryObserver(queryClient, { queryKey, ...options }),
[queryClient, queryKey],
);
React.useEffect(() => {
const unsubscribe = observer.subscribe((result) => {
setResult(result);
});
return unsubscribe;
});
return result;
} |
Beta Was this translation helpful? Give feedback.
-
OK thanks again - I saw your answer here and combined with the question, it led me to believe that I should be using I suppose that I could just replace Still trying to decide, but I'm heavily leaning towards RTK-Query at this time because they have a hook |
Beta Was this translation helpful? Give feedback.
-
Having the component be dependent on redux state is not a problem for me. The way I see it, every data-bound component is going to be dependent on something whether it's react-query, rtk-query, redux thunk action/selector, a HOC or just plain props passed from the parent and some degree setup is involved with each of those. On the other hand, looking at the react-query examples I see that setting up a react-query environment is quite a bit simpler than setting up a redux store. So, with that in mind perhaps I'll give react-query another look and see if I can't make it work for me. |
Beta Was this translation helpful? Give feedback.
what I don't like about
queryClient.getQueryData
is two things:<Child1A>
is not rendered under<Parent>
anymore - it will not work.<Providers>
here on purpose :)) - they are mean…