Replies: 2 comments 5 replies
-
Per https://redux.js.org/usage/migrations/migrating-rtk-2#preloadedstate-type-removed-in-favour-of-reducer-generic , the
|
Beta Was this translation helpful? Give feedback.
2 replies
-
Thanks for looking into this. Sure, below is my reducer. I have some more complicated ones, but I deleted all other reducers and the error occurs when using only this one: import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { RootState } from "RootTypes"
interface DesignerState {
editing: boolean
menuTimestamp: number
selectedItem: string | null
}
export const initialState: DesignerState = {
editing: false,
menuTimestamp: Date.now(),
selectedItem: null,
}
export const designerSlice = createSlice({
name: "designer",
initialState,
reducers: {
refreshMenu(state) {
state.menuTimestamp = Date.now()
},
setEditing(state, action: PayloadAction<boolean>) {
state.editing = action.payload
},
setSelectedItem(state, action: PayloadAction<string | null>) {
state.selectedItem = action.payload
},
},
})
export const { refreshMenu, setEditing, setSelectedItem } = designerSlice.actions
export default designerSlice.reducer
export const selectEditing = (state: RootState) => state.designer.editing
export const selectMenuTimestamp = (state: RootState) => state.designer.menuTimestamp
export const selectSelectedItem = (state: RootState) => state.designer.selectedItem Here is how I define declare module "RootTypes" {
// Defined here to prevent circular dependencies
// with help from https://stackoverflow.com/a/66324900/166229
import { AnyAction, StateFromReducersMapObject, ThunkAction } from "@reduxjs/toolkit"
export type RootState = StateFromReducersMapObject<typeof import("../src/state/store").reducer>
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, any, AnyAction>
} And here is how I try to initialize the store now: export function initStore(preloadedState?: Partial<RootState>) {
const store = configureStore({
reducer: {
designer: designerReducer,
},
preloadedState,
})
return store
} with the error:
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In RTK 1.x I used the following code to initialize a store in a Next.js app:
In RTK 2.x there is no
PreloadedState
andCombinedState
anymore. Unfortunately, I don't fully understand the migration guide. I tried to usePartial<RootState>
instead ofPreloadedState<CombinedState<RootState>>
, but that does not seem to be accepted with errors like this forreducer
andmiddleware
:Only using
RootState
works, but I don't want to preload the full state, but only some slices. What else could I try?Beta Was this translation helpful? Give feedback.
All reactions