: Set a very low keepUnusedDataFor (e.g., 0) on the endpoint to ensure the blob is removed from the store immediately after the component unmounts.
: Keep all your network requests within a single API slice for better maintainability. Step 1: Create the API Endpoint
import createApi, fetchBaseQuery from '@reduxjs/toolkit/query/react'; export const filesApi = createApi( reducerPath: 'filesApi', baseQuery: fetchBaseQuery( baseUrl: '/api' ), endpoints: (builder) => ( downloadFile: builder.query( query: (fileId) => ( url: `download/$fileId`, // This is the key: set responseHandler to 'blob' responseHandler: (response) => response.blob(), ), // Recommendation: Prevent large files from clogging Redux state // by keeping cache time short or not caching at all. ), ), ); export const useDownloadFileQuery, useLazyDownloadFileQuery = filesApi; Use code with caution. Step 2: Triggering the Browser Download
Using RTK Query what is the best practice to download a file
: Use generated hooks (e.g., isLoading , isError ) to provide visual feedback to the user.
Sie haben Ihr Passwort vergessen?