Filtering through returned data
Where should I be filtering through the data, what's best practice? Would it be onSuccess? At the moment I am doing it like this:
18 Replies
ambitious-aqua•4y ago
Hi, it all depends on what data you would like to cache, how your API allows you to filter etc.
With the code above, the data that would be cached would be the filtered one
another solution
You retrieve all your employers and you define 2 hooks: one to retrieve all employers, another one which uses the same queryKey with a
select to apply the filters.vicious-goldOP•4y ago
Ok, I will investigate, thanks for the reply 🙂
ambitious-aqua•4y ago
Don't forget to read https://tkdodo.eu/blog/react-query-data-transformations while investigating 😉
React Query Data Transformations
Learn the possibilities to perform the quite common and important task of transforming your data with react-query
vicious-goldOP•4y ago
Taking into consideration that if I do the filtering in the fetchEmployers useQuery, it kinda defeats the hooks re-usablility. So my naive approach is to filter through the data in the component instead.
I will have a read of the docs on how to use select`to replicate
Or, I can create a new hook with select and call that in the component, so we have 2 hooks. 1 to fetch the Employers which we can re-use.
another hook to do the filtering.
ambitious-aqua•4y ago
You should also rename your hooks
prefix them with
usevicious-goldOP•4y ago
Good point. Thanks 🙂
ambitious-aqua•4y ago
Also you could factorise the query config (queryKey, queryFn) between the 2 hooks to make sure they stay in sync
vicious-goldOP•4y ago
Where is a typical place you would store the queryFn's?
For example, in another file in utils?
ambitious-aqua•4y ago
"it depends"
ambitious-aqua•4y ago
We took inspiration from https://tkdodo.eu/blog/effective-react-query-keys to build our own key factories
Effective React Query Keys
Learn how to structure React Query Keys effectively as your App grows
vicious-goldOP•4y ago
I want to re-use the queryFn's but run into an error that I can't seem to workout.
fetchEmployees is the queryFn that is used in both useFetchEmployers and useFetchEmployersQuery. This makes sense to me and should work but I get the the below error.
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
Not sure if I am missing something obvious?
ambitious-aqua•4y ago
Is this used in a component ?
I mean this line: const { data: employersData } = useFetchEmployersQuery();
vicious-goldOP•4y ago
Yep
ambitious-aqua•4y ago
hum strange
Could you provide more code sample to help understanding ?
vicious-goldOP•4y ago
Sure.
Component:
Hook File
ambitious-aqua•4y ago
Strange
vicious-goldOP•4y ago
Yeah, can't work it out
ambitious-aqua•4y ago
Could you try reproducing in a codesandbox?