-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Labels
Milestone
Description
Hello there. I have recently started using RTK query to fetch and cache my data in my frontend and I have some questions. My use case is the following:
- I have some filters on my app
- When a filter changes, a new request is executed to fetch resources
- The current selection of the filters are stored in redux
- For each filter, more than one values might be selected and as a result each one of them is an array of selected values.
- I want to use RTK in order to cache the responses for each selection of filters
- I use
queryFn
instead ofquery
since I fetch data in a custom way
The api is defined as following:
export const resourcesApi = createApi({
reducerPath: 'resourcesApi',
endpoints: (builder) => ({
getResources: builder.query({
queryFn: ({selectedFilter1, selectedFilter2}) => {
// this function fetches the data
return getResourcesBasedOnFilters(selectedFilter1, selectedFilter2)
},
}),
}),
})
export const { useGetResourcesQuery } = resourcesApi
And I call it using:
export default function Resources(props){
let selectedFilter1 = useSelector((state) => state.filters.selectedFilter1)
let selectedFilter2 = useSelector((state) => state.filters.selectedFilter2)
const {isLoading, isFetching, data} = useGetResourcesQuery({selectedFilter1, selectedFilter2})
if (isLoading){
// show loader
}
else{
// show data
}
}
My questions are the following:
- The more filters I have, the slower redux becomes. Can I do something for it?
- Can I access the filters directly in queryFn ?