使用 React.lazy 时未捕获的未定义错误
Uncaught undefined error when using React.lazy
我正在尝试实施 Route-based code splitting,如 React 文档中所述。
这是我在添加惰性实现之前的应用。这很好用:
import Counter from "./Counter";
import Home from "./Home";
import Login from "./Login";
export function App() {
return (
<Router>
<Suspense fallback={<div>"Loading.."</div>}>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/login" component={Login} />
<Route exact path="/counter" component={Counter} />
</Switch>
</Router>
);
}
我所做的改变是我用这个替换了 3 个导入:
import { lazy, Suspense } from "react";
const Home = lazy(() => import("./Home"));
const Login = lazy(() => import("./Login"));
const Counter = lazy(() => import("./Counter"));
此代码构建成功,但浏览器上没有呈现任何内容,我在控制台中收到此错误:
Uncaught undefined
The above error occurred in one of your React components:
in Unknown (created by Context.Consumer)
in Route (at App.tsx:29)
in Switch (at App.tsx:28)
in Suspense (at App.tsx:27)
in Router (created by BrowserRouter)
in BrowserRouter (at App.tsx:25)
in ErrorBoundary (at App.tsx:24)
in App (at src/index.tsx:19)
in Provider (at src/index.tsx:18)
in StrictMode (at src/index.tsx:17)
我是不是做错了什么?
附加上下文:
如果重要的话,这些组件被命名为默认重新导出的组件,因为这是 React.lazy 所要求的:
export { Home as default } from "./Home";
其中一个组件使用 redux,因此应用程序被包装在商店提供者中:
<Provider store={store}>
<App />
</Provider>
这是我的 tsconfig:
{
"compilerOptions": {
"baseUrl": ".",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"target": "es5",
"module": "esnext"
},
"include": [
"src"
]
}
和包版本:
"react": "^17.0.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.1",
"react-scripts": "^4.0.1",
"react-router-dom": "^5.2.0",
"typescript": "^4.1.2",
这是source code and possibly related issue on GitHub。
在查看了我自己的问题之后,我通过查看依赖项弄明白了。我的 react
和 react-dom
在不同的主要版本上。
这解决了它:
npm i -D react-dom@17.0.1
我也遇到了这个问题,结果是抛出错误是因为我的导入语句有括号,这意味着我没有返回任何内容
const MyComponent = React.lazy(() => {
import('./scenes/MyComponent')
)};
删除括号后一切正常,因为我实际上并没有返回任何东西。
const MyComponent = React.lazy(() =>
import('./scenes/MyComponent')
);
我正在尝试实施 Route-based code splitting,如 React 文档中所述。
这是我在添加惰性实现之前的应用。这很好用:
import Counter from "./Counter";
import Home from "./Home";
import Login from "./Login";
export function App() {
return (
<Router>
<Suspense fallback={<div>"Loading.."</div>}>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/login" component={Login} />
<Route exact path="/counter" component={Counter} />
</Switch>
</Router>
);
}
我所做的改变是我用这个替换了 3 个导入:
import { lazy, Suspense } from "react";
const Home = lazy(() => import("./Home"));
const Login = lazy(() => import("./Login"));
const Counter = lazy(() => import("./Counter"));
此代码构建成功,但浏览器上没有呈现任何内容,我在控制台中收到此错误:
Uncaught undefined
The above error occurred in one of your React components:
in Unknown (created by Context.Consumer)
in Route (at App.tsx:29)
in Switch (at App.tsx:28)
in Suspense (at App.tsx:27)
in Router (created by BrowserRouter)
in BrowserRouter (at App.tsx:25)
in ErrorBoundary (at App.tsx:24)
in App (at src/index.tsx:19)
in Provider (at src/index.tsx:18)
in StrictMode (at src/index.tsx:17)
我是不是做错了什么?
附加上下文:
如果重要的话,这些组件被命名为默认重新导出的组件,因为这是 React.lazy 所要求的:
export { Home as default } from "./Home";
其中一个组件使用 redux,因此应用程序被包装在商店提供者中:
<Provider store={store}>
<App />
</Provider>
这是我的 tsconfig:
{
"compilerOptions": {
"baseUrl": ".",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"target": "es5",
"module": "esnext"
},
"include": [
"src"
]
}
和包版本:
"react": "^17.0.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.1",
"react-scripts": "^4.0.1",
"react-router-dom": "^5.2.0",
"typescript": "^4.1.2",
这是source code and possibly related issue on GitHub。
在查看了我自己的问题之后,我通过查看依赖项弄明白了。我的 react
和 react-dom
在不同的主要版本上。
这解决了它:
npm i -D react-dom@17.0.1
我也遇到了这个问题,结果是抛出错误是因为我的导入语句有括号,这意味着我没有返回任何内容
const MyComponent = React.lazy(() => {
import('./scenes/MyComponent')
)};
删除括号后一切正常,因为我实际上并没有返回任何东西。
const MyComponent = React.lazy(() =>
import('./scenes/MyComponent')
);