Closed
Description
Details
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?