I need to make a request to a URL like this /common/path/:userId/specific/path/endpoints
. /common/path
is shared across many endpoints, and I don't want to put all of those queries in the same file. Ideally, I'd create a slice for /common/path/:userId/specific/path
and its endpoints. I have userId when the store is created. Could I pass userId into the slice and put it in the baseUrl somehow?
Or am I approaching this wrong? Is there a better way? I can't change the structure of the URL.
EDIT: This is what I'm trying.
In MyFile.jsx
const store = configureStore({reducer: mySlice, userId})
In mySlice.js
export const mySlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: `/common/path/${state.userId}/specific/path`
}),
endpoints: {4 endpoints defined here}
})
But this doesn't work, because state
and getState
are not accessible when creating the baseUrl.
baseURL
string value when the slice and state are created? Do you have a minimal reproducible example of the code you are working with?