笔记二十六、React中路由懒加载的扩展使用

26.1 在路由中配置懒加载 lazy

routes/index.jsx

代码

import {Navigate} from "react-router-dom";
import Home from "../components/Home";
import About from "../components/About";
// import Classify from "../components/Home/components/Classify.jsx";
// import Navigation from "../components/Home/components/Navigation.jsx";
import {lazy} from "react";

//动态引入路径传入lazy函数
const Classify = lazy(() => import("../components/Home/components/Classify"));
const Navigation = lazy(() => import("../components/Home/components/Navigation"));

export default [
    {
        path: '/home',
        element: <Home/>,
        children: [
            {
                path: 'classify',
                element: <Classify/>
            },
            {
                path: 'navigation',
                element: <Navigation/>
            },
        ]
    },
    {
        path: '/about',
        element: <About/>,
    },
    {
        path: '/',
        element: <Navigate to='about'/>,
    }
]

26.2 Home/index.jsx 在父组件中使用 Suspense 

代码

import React, {Suspense} from "react";
import {NavLink, Outlet, useNavigate} from "react-router-dom";

const Home = () => {

    // 类组件中不能用const定义变量
    // 选中高亮
    const activeStyle = ({isActive}) => {
        return isActive ? 'background' : "";
    };

    // 编程式路由导航
    const navigate = useNavigate();
    const toClassify = () => {
        navigate('classify', {state: {param_C: 'elendalee', param_D: 20}})
    };

    return (
        <div>
            首页的页面
            <div style={
  {display: "flex", justifyContent: 'center', marginTop: '20px'}}>
                <button onClick={toClassify}>classify</button>
                <NavLink to='navigation' className={activeStyle}>navigation</NavLink>
            </div>
            <div style={
  {background: 'red'}}>
                {/*fallback 兜底样式loading...*/}
                <Suspense fallback={<h2>loading...</h2>}>
                    {/*<!-- Renders the child route's element, if there is one. -->*/}
                    <Outlet/>
                </Suspense>
            </div>
        </div>);

}

export default Home;

 

 

相关推荐

  1. vue

    2023-12-08 04:14:01       20 阅读
  2. 2023-12-08 04:14:01       7 阅读
  3. React和Vue实现

    2023-12-08 04:14:01       32 阅读
  4. React和Vue实现

    2023-12-08 04:14:01       27 阅读
  5. React和Vue实现

    2023-12-08 04:14:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 04:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 04:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 04:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 04:14:01       20 阅读

热门阅读

  1. ❀My学习Linux命令小记录(15)❀

    2023-12-08 04:14:01       26 阅读
  2. 设计多级菜单的数据结构(C语言实现)

    2023-12-08 04:14:01       37 阅读
  3. 简述本人项目中常用的Typescript的知识

    2023-12-08 04:14:01       35 阅读
  4. 括号匹配的检验(数据结构实训)

    2023-12-08 04:14:01       30 阅读
  5. K8s基础

    K8s基础

    2023-12-08 04:14:01      39 阅读
  6. chatgpt用到哪些算法

    2023-12-08 04:14:01       36 阅读
  7. ElasticSearch之cat nodeattrs API

    2023-12-08 04:14:01       43 阅读
  8. 将linux服务器 设置成 proxy.SOCKS5 服务器

    2023-12-08 04:14:01       35 阅读