• React 全栈体系(十八)


    第九章 React Router 6

    二、代码实战

    6. 路由的 params 参数

    请添加图片描述

    6.1 routes
    /* src/routes/index.js */
    import About from "../pages/About";
    import Home from "../pages/Home";
    import Message from "../pages/Message";
    import News from "../pages/News";
    import Detail from "../pages/Detail";
    import { Navigate } from "react-router-dom";
    
    export default [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/home",
        element: <Home />,
        children: [
          {
            path: "news",
            element: <News />,
          },
          {
            path: "message",
            element: <Message />,
            children: [
              {
                path: "detail/:id/:title/:content",
                element: <Detail />,
              },
            ],
          },
        ],
      },
      {
        path: "/",
        element: <Navigate to="/about" />,
      },
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    6.2 Detail.jsx
    /* src/pages/Detail.jsx */
    import React from "react";
    import { useParams } from "react-router-dom";
    
    export default function Detail() {
      const { id, title, content } = useParams();
      // const x = useMatch('/home/message/detail/:id/:title/:content')
      // console.log(x)
      return (
        <ul>
          <li>消息编号:{id}</li>
          <li>消息标题:{title}</li>
          <li>消息内容:{content}</li>
        </ul>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    6.3 Message.jsx
    /* src/pages/Message.jsx */
    import React, { useState } from "react";
    import { Link, Outlet } from "react-router-dom";
    
    export default function Message() {
      const [messages] = useState([
        { id: "001", title: "消息1", content: "锄禾日当午" },
        { id: "002", title: "消息2", content: "汗滴禾下土" },
        { id: "003", title: "消息3", content: "谁知盘中餐" },
        { id: "004", title: "消息4", content: "粒粒皆辛苦" },
      ]);
      return (
        <div>
          <ul>
            {messages.map((m) => {
              return (
                // 路由链接
                <li key={m.id}>
                  <Link to={`detail/${m.id}/${m.title}/${m.content}`}>
                    {m.title}
                  </Link>
                </li>
              );
            })}
          </ul>
          <hr />
          {/* 指定路由组件的展示位置 */}
          <Outlet />
        </div>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    7. 路由的 search 参数

    请添加图片描述

    7.1 routes
    /* src/routes/index.js */
    import About from "../pages/About";
    import Home from "../pages/Home";
    import Message from "../pages/Message";
    import News from "../pages/News";
    import Detail from "../pages/Detail";
    import { Navigate } from "react-router-dom";
    
    export default [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/home",
        element: <Home />,
        children: [
          {
            path: "news",
            element: <News />,
          },
          {
            path: "message",
            element: <Message />,
            children: [
              {
                path: "detail",
                element: <Detail />,
              },
            ],
          },
        ],
      },
      {
        path: "/",
        element: <Navigate to="/about" />,
      },
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    7.2 Detail.jsx
    /* src/pages/Detail.jsx */
    import React from "react";
    import { useSearchParams, useLocation } from "react-router-dom";
    
    export default function Detail() {
      const [search, setSearch] = useSearchParams();
      const id = search.get("id");
      const title = search.get("title");
      const content = search.get("content");
      const x = useLocation();
      console.log("@", x);
      return (
        <ul>
          <li>
            <button onClick={() => setSearch("id=008&title=哈哈&content=嘻嘻")}>
              点我更新一下收到的search参数
            </button>
          </li>
          <li>消息编号:{id}</li>
          <li>消息标题:{title}</li>
          <li>消息内容:{content}</li>
        </ul>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    7.3 Message.jsx
    /* src/pages/Message.jsx */
    import React,{useState} from 'react'
    import {Link,Outlet} from 'react-router-dom'
    
    export default function Message() {
    	const [messages] = useState([
    		{id:'001',title:'消息1',content:'锄禾日当午'},
    		{id:'002',title:'消息2',content:'汗滴禾下土'},
    		{id:'003',title:'消息3',content:'谁知盘中餐'},
    		{id:'004',title:'消息4',content:'粒粒皆辛苦'}
    	])
    	return (
    		<div>
    			<ul>
    				{
    					messages.map((m)=>{
    						return (
    							// 路由链接
    							<li key={m.id}>
    								<Link to={`detail?id=${m.id}&title=${m.title}&content=${m.content}`}>{m.title}</Link>
    							</li>
    						)
    					})
    				}
    			</ul>
    			<hr />
    			{/* 指定路由组件的展示位置 */}
    			<Outlet />
    		</div>
    	)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    8. 路由的 state 参数

    8.1 routes
    /* src/routes/index.js */
    import About from "../pages/About";
    import Home from "../pages/Home";
    import Message from "../pages/Message";
    import News from "../pages/News";
    import Detail from "../pages/Detail";
    import { Navigate } from "react-router-dom";
    
    export default [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/home",
        element: <Home />,
        children: [
          {
            path: "news",
            element: <News />,
          },
          {
            path: "message",
            element: <Message />,
            children: [
              {
                path: "detail",
                element: <Detail />,
              },
            ],
          },
        ],
      },
      {
        path: "/",
        element: <Navigate to="/about" />,
      },
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    8.2 Detail.jsx
    /* src/pages/Detail.jsx */
    import React from "react";
    import { useLocation } from "react-router-dom";
    
    export default function Detail() {
      const {
        state: { id, title, content },
      } = useLocation();
      return (
        <ul>
          <li>消息编号:{id}</li>
          <li>消息标题:{title}</li>
          <li>消息内容:{content}</li>
        </ul>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    8.3 Message.jsx
    /* src/pages/Message.jsx */
    import React, { useState } from "react";
    import { Link, Outlet } from "react-router-dom";
    
    export default function Message() {
      const [messages] = useState([
        { id: "001", title: "消息1", content: "锄禾日当午" },
        { id: "002", title: "消息2", content: "汗滴禾下土" },
        { id: "003", title: "消息3", content: "谁知盘中餐" },
        { id: "004", title: "消息4", content: "粒粒皆辛苦" },
      ]);
      return (
        <div>
          <ul>
            {messages.map((m) => {
              return (
                // 路由链接
                <li key={m.id}>
                  <Link
                    to="detail"
                    state={{
                      id: m.id,
                      title: m.title,
                      content: m.content,
                    }}
                  >
                    {m.title}
                  </Link>
                </li>
              );
            })}
          </ul>
          <hr />
          {/* 指定路由组件的展示位置 */}
          <Outlet />
        </div>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    9. 编程式路由导航

    请添加图片描述

    9.1 Header.jsx
    /* src/components/Header.jsx */
    import React from "react";
    import { useNavigate } from "react-router-dom";
    
    export default function Header() {
      const navigate = useNavigate();
    
      function back() {
        navigate(-1);
      }
      function forward() {
        navigate(1);
      }
    
      return (
        <div className="col-xs-offset-2 col-xs-8">
          <div className="page-header">
            <h2>React Router Demo</h2>
            <button onClick={back}>←后退</button>
            <button onClick={forward}>前进→</button>
          </div>
        </div>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    9.2 Message.jsx
    /* src/pages/Message.jsx */
    import React, { useState } from "react";
    import { Link, Outlet, useNavigate } from "react-router-dom";
    
    export default function Message() {
      const navigate = useNavigate();
      const [messages] = useState([
        { id: "001", title: "消息1", content: "锄禾日当午" },
        { id: "002", title: "消息2", content: "汗滴禾下土" },
        { id: "003", title: "消息3", content: "谁知盘中餐" },
        { id: "004", title: "消息4", content: "粒粒皆辛苦" },
      ]);
    
      function showDetail(m) {
        navigate("detail", {
          replace: false,
          state: {
            id: m.id,
            title: m.title,
            content: m.content,
          },
        });
      }
      return (
        <div>
          <ul>
            {messages.map((m) => {
              return (
                // 路由链接
                <li key={m.id}>
                  <Link
                    to="detail"
                    state={{
                      id: m.id,
                      title: m.title,
                      content: m.content,
                    }}
                  >
                    {m.title}
                  </Link>
                  <button onClick={() => showDetail(m)}>查看详情</button>
                </li>
              );
            })}
          </ul>
          <hr />
          {/* 指定路由组件的展示位置 */}
          <Outlet />
        </div>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    9.3 App.jsx
    /* src/App.jsx */
    import React from "react";
    import { NavLink, useRoutes } from "react-router-dom";
    import routes from "./routes";
    import Header from "./components/Header";
    
    export default function App() {
      //根据路由表生成对应的路由规则
      const element = useRoutes(routes);
      return (
        <div>
          <div className="row">
            <Header />
          </div>
          <div className="row">
            <div className="col-xs-2 col-xs-offset-2">
              <div className="list-group">
                {/* 路由链接 */}
                <NavLink className="list-group-item" to="/about">
                  About
                </NavLink>
                <NavLink className="list-group-item" to="/home">
                  Home
                </NavLink>
              </div>
            </div>
            <div className="col-xs-6">
              <div className="panel">
                <div className="panel-body">
                  {/* 注册路由 */}
                  {element}
                </div>
              </div>
            </div>
          </div>
        </div>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    (附源码)php投票系统 毕业设计 121500
    (附源码)springboot通用数据展示系统 毕业设计 200934
    《教练型管理者》读书笔记-第4篇【教练实践】
    平均数的增长百分比
    业务安全情报第22期 | 不法分子为何盗刷企业短信?
    为什么要做字节对齐 alignment?
    GO语言开山篇(二):诞生小故事
    基于springboot的社区医院管理系统的设计
    【前端系列】前端如何使用websocket发送消息
    Java异常
  • 原文地址:https://blog.csdn.net/sgsgkxkx/article/details/133327107