【从0搭建react后台管理】10.react页面跳转方式

本文介绍了在React-Routerv6之前的版本中,声明式导航和编程式导航的不同方法,包括使用match.params,location.state,location.search以及新的useNavigatehook。同时展示了在最新版v6中如何使用useLocation,useSearchParams和useParams进行导航和参数传递。
摘要由CSDN通过智能技术生成

【项目地址】
https://gitee.com/shangqiao1996/member-points-mgmt


一、声明式导航

(react-router@6 之前)

[match]
<NavLink to='/layout/First/zhangsan/18'>First</NavLink>
<Route path="/layout/First/:name/:age" component={First}></Route>;
this.props.match.params;
// {
//   "name": "zhangsan",
//	  "age": "18"
// }

<NavLink
  to={{
    pathname: '/layout/Third',
    state: { name: 'zhangsan', age: 18 }
  }}
></NavLink>
this.props.location.state
// {
//     "name": "zhangsan",
//     "age": 18
// }
二、编程式导航
  1. 操作 history(react-router@6 之前)

    [location.search]
    this.props.history.push('/layout/Fifth?name=zhangsan&age=18')
    this.props.location.search
    // ?name=zhangsan&age=18
    
    [location.state]
    this.props.history.push('/layout/Sixth', { name: 'zhangsan', age: 18 })
    this.props.location.state
    // {
    //     "name": "zhangsan",
    //     "age": 18
    // }
    
    [match]
    this.props.history.push('/layout/Fourth/zhangsan/18')
    // 需要配置路由
    <Route path='/layout/Fourth/:name/:age' component={Fourth}></Route>
    this.props.match.params
    // {
    //      "name": "zhangsan",
    //      "age": "18"
    // }
    
    [location.query]
    this.props.history.push({
       pathname: '/layout/Seventh',
       query: {
         name: 'zhangsan'age: 18
       }
    })
    this.props.location.query
    // {
    //     "name": "zhangsan",
    //     "age": 18
    // }
    
  2. useNavigate()(react-router@6)【官方推荐】

    import { useNavigate, useLocation, useParams } from "react-router";
    import { useSearchParams } from "react-router-dom";
    
    const location = useLocation();
    const [searchParams] = useSearchParams();
    const params = useParams();
    
    // useNaviagte()('/points-get/promotion-points', { state: { name: 'shangqiao' } })
    console.log("location", location.state);
    // navigate('/points-get/promotion-points?name=shangqiao&age=20');
    console.log("searchParams", searchParams.getAll("name"));
    // navigate('/points-get/promotion-points/shangqiao');
    console.log("params", params);
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪 风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值