尝试使用 React Route 和 Webpack 实现用于代码拆分的 React "Lazy" 路由
Trying to implement a React "Lazy" route for code splitting with React Route and Webpack
我正在尝试实施“惰性路由”以在我的 React 单页应用程序中实现代码拆分。
- 都是客户端渲染的
- 我正在使用
react-router
- 也在使用
Webpack
我有这个组件可以呈现我的所有路线。
AllRoutes.js
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
// AND SO ON
</Switch>
);
}
我正在尝试做这样的事情:
import LazyRoute from './LazyRoute';
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
<Route exact path={"/ROUTE_C"} component={LazyRoute}/> // THIS SHOULD LAZY LOAD
</Switch>
);
}
这是我尝试过的:
LazyRoute.js
function LazyRoute() {
return import("@pages/Component_C").then(({default: Component_C}) => {
return Component_C;
});
}
export default LazyRoute;
Webpack 似乎在拆分这个 LazyRoute
包中发挥了作用:
但是我收到这个错误:
我知道我正在通过 return import()
重新调整 Promise
。但这不是重点吗?
您好,您可以在这里使用 React Lazy。这是一个应该可以使用您的实现的示例。
import React, { lazy } from "react";
import { Route, Switch } from "react-router-dom";
import LazyRoute from './LazyRoute';
const Component_C = lazy(() => import("./Component_C"));
function AllRoutes() {
return (
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
<Route exact path={"/ROUTE_C"} component={LazyRoute(Component_C)}/> // THIS SHOULD LAZY LOAD
</Switch>
);
}
LazyRoute.js
import React, { Suspense } from "react";
function LazyRoute(Component) {
return props => (
<Suspense fallback={<div>Loading...</div>}>
<Component {...props} />
</Suspense>
);
}
export default LazyRoute;
刚找到我为此编写的旧解决方案。
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
const LS = {};
LS.Container_DIV = styled.div`
`;
async function lazyRender() {
const TestContainer = (await import("@pages/Test/TestContainer"));
return new Promise((resolve) => {
resolve(TestContainer);
});
}
function LazyRoute(props) {
console.log('Rendering LazyRoute...');
const [lazyLoading,setLazyLoading] = useState(true);
const [LazyComponent,setLazyComponent] = useState(null);
useEffect(() => {
async function getLazyComponent() {
const component = await lazyRender();
setLazyComponent(component);
setLazyLoading(false);
}
getLazyComponent();
},[]);
return(
lazyLoading ?
<div>I am Lazy Loading....</div>
: <LazyComponent.default {...props}/>
);
}
export default React.memo(LazyRoute);
我正在尝试实施“惰性路由”以在我的 React 单页应用程序中实现代码拆分。
- 都是客户端渲染的
- 我正在使用
react-router
- 也在使用
Webpack
我有这个组件可以呈现我的所有路线。
AllRoutes.js
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
// AND SO ON
</Switch>
);
}
我正在尝试做这样的事情:
import LazyRoute from './LazyRoute';
function AllRoutes() {
return(
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
<Route exact path={"/ROUTE_C"} component={LazyRoute}/> // THIS SHOULD LAZY LOAD
</Switch>
);
}
这是我尝试过的:
LazyRoute.js
function LazyRoute() {
return import("@pages/Component_C").then(({default: Component_C}) => {
return Component_C;
});
}
export default LazyRoute;
Webpack 似乎在拆分这个 LazyRoute
包中发挥了作用:
但是我收到这个错误:
我知道我正在通过 return import()
重新调整 Promise
。但这不是重点吗?
您好,您可以在这里使用 React Lazy。这是一个应该可以使用您的实现的示例。
import React, { lazy } from "react";
import { Route, Switch } from "react-router-dom";
import LazyRoute from './LazyRoute';
const Component_C = lazy(() => import("./Component_C"));
function AllRoutes() {
return (
<Switch>
<Route exact path={"/ROUTE_A"} component={Component_A}/>
<Route exact path={"/ROUTE_B"} component={Component_B}/>
<Route exact path={"/ROUTE_C"} component={LazyRoute(Component_C)}/> // THIS SHOULD LAZY LOAD
</Switch>
);
}
LazyRoute.js
import React, { Suspense } from "react";
function LazyRoute(Component) {
return props => (
<Suspense fallback={<div>Loading...</div>}>
<Component {...props} />
</Suspense>
);
}
export default LazyRoute;
刚找到我为此编写的旧解决方案。
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
const LS = {};
LS.Container_DIV = styled.div`
`;
async function lazyRender() {
const TestContainer = (await import("@pages/Test/TestContainer"));
return new Promise((resolve) => {
resolve(TestContainer);
});
}
function LazyRoute(props) {
console.log('Rendering LazyRoute...');
const [lazyLoading,setLazyLoading] = useState(true);
const [LazyComponent,setLazyComponent] = useState(null);
useEffect(() => {
async function getLazyComponent() {
const component = await lazyRender();
setLazyComponent(component);
setLazyLoading(false);
}
getLazyComponent();
},[]);
return(
lazyLoading ?
<div>I am Lazy Loading....</div>
: <LazyComponent.default {...props}/>
);
}
export default React.memo(LazyRoute);