【问题标题】:canot find the module 'react' in nexjs application在下一个 js 应用程序中找不到模块 \'react\'
【发布时间】:2023-01-30 02:26:59
【问题描述】:

我一直在我的 next.js 应用程序上设置 redux,但我不断收到此错误。我无法检测到它来自哪里以及出于什么原因。我使用的是 next.js 13 版本,应用程序目录未启用。下面是我为了在我的应用程序中添加 redux 而编写的屏幕截图和代码。

_app.tsx

import "../styles/globals.css";
import type { AppProps } from "next/app";
import Layout from "components/Layout/Layout";
import { Provider } from "react-redux";
// import { PersistGate } from "redux-persist/integration/react";
import store from "store/store";
import React from "react";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <Provider store={store}>
      {/* <PersistGate loading={<>Loading...</>} persistor={persistor}> */}
        <Layout>
          <Component {...pageProps} />
        </Layout>
      {/* </PersistGate> */}
    </Provider>
  );
}

商店.ts

import { configureStore } from "@reduxjs/toolkit";
import rootReducer from "reducers/root-reducer";
// import rootReducer from "reducers/root-reducer";
// import { persistStore, persistReducer } from "redux-persist";
// import storage from "redux-persist/lib/storage";
// import React from "react";

// const persistConfig = { key: "root", storage };

// const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = configureStore({
  reducer: rootReducer,
});

export default store;
// export const persistor = persistStore(store);

你看到的注释代码是我为了检查错误是否消失但它仍然存在而做的。问题是什么?

【问题讨论】:

  • 您需要导入 react 才能使 react-redux Provider 正常工作。为此,请在文件顶部添加以下行: import React from 'react';
  • 你检查过你的 package.json 或重新安装包了吗?

标签: javascript reactjs redux next.js redux-toolkit


【解决方案1】:

React 已导入文件中。解决问题的是动态导入。谢谢

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 2020-08-05
    • 2021-10-07
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多