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

[React] Route-based Splitting

时间:2022-08-26 20:34:50浏览次数:77  
标签:lazy based Route React import const

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")

 

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

相关文章

  • [React] Import on Visibility
    Source:https://javascriptpatterns.vercel.app/patterns/performance-patterns/import-on-visibility Onewaytodynamicallyimportcomponentsoninteraction,is......
  • [React] Compound Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/react-patterns/compound-pattern Acompoundcompoenntusagelookslikethis:importReactfrom"react......
  • [React] SWR for data fetching
    DocsThename“SWR”isderivedfrom stale-while-revalidate,aHTTPcacheinvalidationstrategypopularizedby HTTPRFC5861.SWRisastrategytofirstret......
  • react中CodeMirror (代码编辑器)
    前言:实现一个在react项目中页面展示代码编辑器的效果。codemirror:使用JavaScript为浏览器实现的多功能文本编辑器。codemirror作用:专门用于编辑代码,并带有实现......
  • React报错之You provided a `checked` prop to a form field
    正文从这开始~总览当我们在多选框上设置了checked属性,却没有onChange处理函数时,会产生"Youprovideda checked proptoaformfieldwithoutan onChange handle......
  • 记录一下react遇到的初始化异步赋值问题
    组件加载时发送接口请求获取数据,在根据收集到的数据的某一项值在进行请求获取相对应的值,实现联动效果1useEffect(()=>{2//getQuestionDetail({id:'61a78f5......
  • react 二级路由嵌套
    嵌套路由之后,静态文静路径错误,更改webpack 打包output输出根目录,publicPath:'/',二级路由刷新之后白屏,在首页模板文件中路径前加  /,   ......
  • localStorge在react中的使用
    1.什么时候用,在哪里用刚获取数据的时候,进行设置,localStorge.setItem(key,value);因为localStorge是用来作为缓存的,且有一定的延时,尤其是在本页面设置本页面使用时,所以,依然......
  • ItemRank: A Random-Walk Based Scoring Algorithm for Recommender Engines
    目录概符号说明本文方法GoriM.andPucciA.ItemRank:arandom-walkbasedscoringalgorithmforrecommenderengines.InInternationalJointConferencesonArt......
  • npm+react linux 开荒
    安装npmyuminstallnodejs.x86_64yuminstallnpm.x86_64 更新GCC版本(参考链接:https://blog.csdn.net/qq_39715000/article/details/120703444)升级到gcc7.3yum-y......