无法解决React Native Redux Toolkit中的模块错误



我在我的App.tsx中添加Redux提供程序时遇到了一些奇怪的错误。我已经安装了所有模块,并做了"删除node_modules文件夹和npm安装"过程已经开始了,但还是不能解决问题

Unable to resolve module ../../../../src/redux from
MY_PROJECT_PATHnode_modules@reduxjstoolkitdistredux-toolkit.cjs.production.min.js:

App.tsx

import { Provider as ReduxProvider } from "react-redux";
import { NavigationContainer } from "@react-navigation/native";
import BottomTabs from "./src/containers/BottomTabs";
import { store } from "./src/redux/store";
export default function App() {
return (
<ReduxProvider store={store}>
<NavigationContainer>
<BottomTabs />
</NavigationContainer>
</ReduxProvider>
);
}

store.ts

import { configureStore } from "@reduxjs/toolkit";
import themeReducer from "./themeSlice";
export const store = configureStore({
reducer: {
theme: themeReducer,
},
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

themeSlice.ts

import { createSlice } from "@reduxjs/toolkit";
import { DEFAULT_DARK_THEME } from "../theme/DefaultDarkTheme";
import {
DEFAULT_LIGHT_THEME,
DEFAULT_LIGHT_THEME_ID,
} from "../theme/DefaultLightTheme";
const messageSlice = createSlice({
name: "theme",
initialState: DEFAULT_LIGHT_THEME,
reducers: {
toggleTheme(state) {
if (state.id === DEFAULT_LIGHT_THEME_ID) return DEFAULT_DARK_THEME;
return DEFAULT_LIGHT_THEME;
},
},
});
export const { toggleTheme } = messageSlice.actions;
export default messageSlice.reducer;

检查你的"依赖项";在包中。Json中没有"@reduxjs/toolkit"终止所有进程并添加"reduxjs/toolkit";第一。

执行"yarn add @reduxjs/toolkit"或者你也可以使用npm

希望这对你有帮助

我也有同样的问题。

也许,您在应用程序的根目录中设置了redux目录,并为在babel.config.js中使用绝对路径设置了别名redux?与纯redux导入名称冲突

在我的例子中,我意识到名称冲突,并删除了设置。但它仍然发生了,然后我可以通过运行expo start -c来清除缓存来解决它。

我正在使用Expo,如果你不使用Expo,react-native start --reset-cache可能会解决这个问题。

最新更新