其他分享
首页 > 其他分享> > [React] Route-based Splitting

[React] Route-based Splitting

作者:互联网

Source: https://javascriptpatterns.vercel.app/patterns/performance-patterns/route-based-splitting

 

If you're using react-router for navigation, you can wrap the Switch component in a React.Suspense, and import the routes using React.lazy. This automatically enables route-based code splitting.

import React, { lazy, Suspense } from "react";
import { Switch, Route, BrowserRouter as Router } from "react-router-dom";

const App = lazy(() => import("./App"));
const About = lazy(() => import("./About"));
const Contact = lazy(() => import("./Contact"));

ReactDOM.render(
  <Router>
    <Suspense fallback={<div>Loading...</div>}>
      <Switch>
        <Route exact path="/">
          <App />
        </Route>
        <Route path="/about">
          <About />
        </Route>
        <Route path="/contact">
          <Contact />
        </Route>
      </Switch>
    </Suspense>
  </Router>,
  document.getElementById("root")

 

标签:react,lazy,based,Route,React,import,const
来源: https://www.cnblogs.com/Answer1215/p/16629083.html