21.1 父组件设置路由参数

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

class App extends React.Component {

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

    render() {
        return (
            <div>
                首页的页面
                <div style={{display: "flex", justifyContent: 'center', marginTop: '20px'}}>
                    <NavLink to={`classify?param_A=${this.state.name}&param_B=${this.state.age}`}
                             className={this.activeStyle}>classify</NavLink>
                    <NavLink to='navigation' className={this.activeStyle}>navigation</NavLink>
                </div>
                <div style={{background: 'red'}}>
                    {/*<!-- Renders the child route's element, if there is one. -->*/}
                    <Outlet/>
                </div>
            </div>);
    }
}

export default App;

 20.2 子组件接收路由参数

import React from 'react';
import {useSearchParams} from "react-router-dom";

const Classify = () => {
    // 第一个参数是接收路由参数,第二个是改变路由参数的方法,可以修改路由参数
    const [search, setSearch] = useSearchParams();
    const param_1 = search.get("param_A");
    const param_2 = search.get("param_B");
    return (
        <div>
            分类的页面
            <div>
                父组件home传递的参数1:{param_1}
                父组件home传递的参数2:{param_2}
            </div>
            <button onClick={() => setSearch('param_A=Leo&param_B=25')}>点击更改</button>
        </div>
    );
}

export default Classify;

路由表不用参数设置

使用路由search进行传递参数 实用情况比较小

11-28 20:43