Skip to content

[RED-14] Using RTK query with multiple parameters retrieved from redux #2467

@kplatis

Description

@kplatis

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 of query 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 ?

RED-14

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions