API Slices: Redux Integration
Internally, createApi
will call the Redux Toolkit createSlice
API to generate a slice reducer and corresponding action creators with the appropriate logic for caching fetched data. It also automatically generates a custom Redux middleware that manages subscription counts and cache lifetimes.
The generated slice reducer and the middleware both need to be added to your Redux store setup in configureStore
in order to work correctly:
reducerPath
Contains the reducerPath
option provided to createApi
. Use this as the root state key when adding the reducer
function to the store so that the rest of the generated API logic can find the state correctly.
reducer
A standard Redux slice reducer function containing the logic for updating the cached data. Add this to the Redux store using the reducerPath
you provided as the root state key.
middleware
A custom Redux middleware that contains logic for managing caching, invalidation, subscriptions, polling, and more. Add this to the store setup after other middleware.