I am trying to set initial state of redux slice with a enum value, but it throws Uncaught TypeError: undefined has no properties
.
The code is:
export const themeTokenSlice = createSlice({
name: "themeToken",
initialState: darkThemeToken,
reducers: {
toggleThemeToken: (state) => {
switch (state.theme) {
case Themes.dark:
state = darkThemeToken;
break;
case Themes.light:
state = lightThemeToken;
break;
default:
return state;
}
}
}
});
export const darkThemeToken: themeState = {
theme: Themes.dark
};
export interface themeState {
theme: Themes;
}
export enum Themes {
dark = "dark",
light = "light",
}
The way it works:
export const darkThemeToken: themeState = {
theme: "dark" as Themes.dark
};
Is there a way to initialize the state with enum?