我正在开发一个应用程序,其中我使用 redux 工具包和 localstorage 来保存主题。
我有这个片段:
import { createSlice } from "@reduxjs/toolkit";
const mode = !localStorage.getItem("theme")
? ""
: JSON.parse(localStorage.getItem("theme")); //the error is on this line
const initialState = {
mode: mode,
};
const ThemeSlice = createSlice({
name: "theme",
initialState,
reducers: {
toggleTheme: (state) => {
state.mode = state.mode === "light" ? "dark" : "light";
},
},
});
export const { toggleTheme } = ThemeSlice.actions;
export default ThemeSlice.reducer;
但它给了我一个错误:Argument of type 'string | null' is not assignable to parameter of type 'string'.
我该怎么做才能消除此错误?