Option to hydrate createApi using extraReducers
See original GitHub issueDetails
Currently, there is no way in which the state fetched on the server using createApi can be persisted on the client. People who are building Nextjs or any SSR application might need this functionality so that we can fetch the data on the server and then re-use the same data on the client-side as well.
I had raised a similar issue on the Next Redux Wrapper repository and one solution to handle this would be to provide an option to add extraReducers in the createApi function that RTKQ exposes. Using that, it would be possible to do something like the following:
const api = createApi({
baseQuery: fetchBaseQuery({
baseUrl: '/',
}),
tagTypes: ['Post'],
endpoints: (build) => ({
// All the endpoints
}),
extraReducers(builder) {
builder.addCase(hydrate, (state, action) => {
console.log('HYDRATE', state, action.payload);
return {
...state,
...(action.payload as any)[api. reducerPath],
};
});
},
})
Right now, the only way to persist createApi is to do what @bjoluc suggested:
configureStore({
reducer: {
[slice.name]: slice.reducer,
[api.reducerPath]: (state, action) => action.type !== HYDRATE ? api.reducer(state, action) : {...state, ...(action.payload as unknown)[api.reducerPath]},
},
},
});
Is there any recommended approach to resolving this issue?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:6 (2 by maintainers)
Top Results From Across the Web
createApi - Redux Toolkit
extractRehydrationInfo A function that is passed every dispatched action. If this returns something other than undefined , that return value ...
Read more >Persistence and Rehydration - Redux Toolkit
Persistence and Rehydration. RTK Query supports rehydration via the extractRehydrationInfo option on createApi .
Read more >Server Side Rendering - Redux Toolkit
In your createApi call, configure rehydration using the extractRehydrationInfo option: ... import { HYDRATE } from 'next-redux-wrapper'
Read more >Customizing createApi - Redux Toolkit
You can create your own versions of createApi by either specifying non-default options for the modules or by adding your own modules.
Read more >Queries - Redux Toolkit
RTK Query > Usage > Queries: fetching data from a server. ... -related options will override the defaults you may have set in...
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
Not yet, also this would theoretically need another step of removing any subscriptions coming in from hydration after a while, when all currently rendered components had an option to re-subscribe to relevant data on the client. Without that step, the re-hydrated data would always be subscribed by the server-side components, which of course will never unmount on the client side.
This is something we will take a look into for the next version of RTK-Query, but not for the first real release.
If you do any experimentation, all insights are very welcome since I don’t have a lot of experience with SSR and Next.
I also need such kind of thing to persist Websocket channel ID to store Example below makes sense if channel ID is determined
But it don’t, so I have to store that ID somewhere and then do