Using RTK query with multiple parameters retrieved from redux
See original GitHub issueHello 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 ?
Issue Analytics
- State:
- Created a year ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Multiple Parameters In Redux-Toolkit Query (RTK Query)
Problem: Redux-Toolkit Query accepts only one argument to pass, but there are some cases where you want to give multiple arguments. Currently, ...
Read more >Queries - Redux Toolkit
RTK Query > Usage > Queries: fetching data from a server. ... If you need to pass in multiple parameters, pass them formatted...
Read more >Redux Toolkit RTK Query sending query parameters
If you need to pass in multiple parameters, pass them formatted as a single "options object". So you can declare the generic parameter...
Read more >Creating React Apps With Redux Toolkit and RTK Query - Toptal
Have you ever wanted to use Redux with features like React Query provides? Now you can, by using the Redux Toolkit and its...
Read more >Using RTK query with multiple parameters : r/reactjs - Reddit
Using RTK query with multiple parameters · I have some filters on my app · When a filter changes, a new request is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello, answering to your questions:
For filters, you can get them this way, its way simple:
you can pass the whole array as arg to use useQuery hook, like this
As for your question, about accessing filters directly inside
queryFn
, based on docs there is a second argument inqueryFn()
, calledapi
, it containsgetState()
, so you can do:To clarify, the idea from my previous comment could look something like this:
Basically subscribing the endpoint to Redux state directly instead of having to go through the component. RTKQ should merge
args
andselection
for the cache key.It might even be a good idea to use
createStructuredSelector
here:For me this would be very helpful to work with app-wide state like auth, landing parameters, global filters.