1.react-router-dom路由框架

create-react-app框架下,页面之间的中转可以使用react-router-dom

官方链接:https://reactrouter.com/en/main/start/overview

2.useSearchParams传参页面跳转

可以使用useNavigateLink跳转

2.1.useNavigate跳转

  • 可以直接在页面?后拼接参数
    • 类似/button_page?name=ksnow&address=beijing&age=18
  • 可以使用URLSearchParams拼接在页面后面,类似以下这种
1
2
3
4
5
6
7
      const queryParams = new URLSearchParams({
        name: "张三",
        age: "18",
        address: "北京",
      });

      navigate(`/button_page?${queryParams.toString()}`);

2.2.Link跳转

1
    <Link to={`/button_page?name=ksnow&address=beijing&age=10`}> 第二种跳转页面</Link>

2.3.完整跳转代码

 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

import { Button } from "antd";
import React from "react";
import { Link, useNavigate } from "react-router-dom";

const HomePage = () => {

  const navigate = useNavigate();

  function handleButtonClick() {
    console.log("click");
    // navigate(`/button_page?name=ksnow&address=beijing&age=10`);

    const queryParams = new URLSearchParams({
      name: "张三",
      age: "18",
      address: "北京",
    });

    navigate(`/button_page?${queryParams.toString()}`);

  }
  return (
          <div>
            <h1>Home Page</h1>
            <Button type="primary" onClick={handleButtonClick}>第一种跳转按钮页面</Button>
            <Link to={`/button_page?name=ksnow&address=beijing&age=10`}> 第二种跳转按钮页面</Link>

          </div>
  );
};

export default HomePage;

2.4.接收跳转参数

查询参数应该通过 useSearchParams 钩子来获取,因此,通过useSearchParams获取参数

 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
import React, { useState } from 'react';
import './ButtonPage.css';
import { useParams, useSearchParams } from 'react-router-dom';

interface ButtonPageProps {

}
    
const ButtonPage: React.FC<ButtonPageProps> = () => {
    ;
    const [size, setSize] = useState('large')
    const [searchParams, setSearchParams] = useSearchParams(); // 获取查询参数

  // 从 searchParams 中读取查询参数
  const name = searchParams.get('name') || '';
  const age = searchParams.get('age') || '0';
  const address = searchParams.get('address') || '';
  console.log(`接收到的参数:name=${name}, age=${age}, address=${address}`);

    const handleSizeChange = (e: any) => {
        setSize(e.target.value);
    };
    return (
        <div>
       
            <p>姓名: {name}</p>
            <p>年龄: {age}</p>
            <p>地址: {address}</p>

        </div>
    )
};

export default ButtonPage

3.路由配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import React from 'react';
import { Routes as ReactRoutes, Route } from 'react-router-dom';
import HomePage from './pages/HomePage';
import PluginsPage from './pages/PluginsPage';
import MinePage from './pages/MinePage';
import ButtonPage from './pages/ButtonPage';

const Routes = () => (
  <ReactRoutes>
    <Route path="/" element={<HomePage />} />
    <Route path="/plugins" element={<PluginsPage />} />
    <Route path="/mine" element={<MinePage />} />
    <Route path="/button_page" element={<ButtonPage />} />
  </ReactRoutes>
);

export default Routes;

4.其它

接下来看看我们试试useParams传参