React react-router-dom 6.0路由使用

由于react路由版本的更新迭代,记录路由知识点

react-router-dom地址,点击查看详情。

下面为使用的例子

  1. Install
   npm install react-router-dom@6 history@5

   yarn add react-router-dom@6 history@5

  1. 配置路由地址
import React from 'react';

import { BrowserRouter as Router, Routes, Route  } from "react-router-dom";

function Routelist(){
return(
<Router>
<Routes>
<Route path='/' element={<App />}></Route>
<Route path="invoices" element={<Invoices />}>
<Route
index
element={
<main style={{ padding: "1rem" }}>
<p>Select an invoice</p>
</main>
}
/>
<Route path=":invoiceId" element={<Invoice />} />
</Route>
{/* 不匹配 */}
<Route
path="*"
element={
<main style={{ padding: "1rem" }}>
<p>There's nothing here!</p>
</main>
}
/>
</Routes>
</Router>
)
}
在使用导航时需要特别注意的时,需要在父组件里面增加 <Outlet /> ,才可以跳转成功。
  1. 跳转页面的方式

    navigate(/invoices/${number}, { state: 1 })

    <NavLink to={/invoices/${number}} state={{a:1}}>1

    在子组件中获取参数

    import { useLocation } from 'react-router-dom'

    let location = useLocation()
    let data=location.state

    地址栏参数获取

    import { useParams } from 'react-router-dom'

    let params = useParams()

    let data=params.get('参数名')

  2. 在class components之前使用withRouter,但在新版中withRouter已移除,如果需要升级更新为 函数式组件或者封装hoc组件

eg:

import React  from "react";
import { useParams, useLocation, useNavigate } from "react-router-dom";
function withRouter(Component) {
return (props) => (
<Component
{...props}
params={useParams()}
location={useLocation()}
navigate={useNavigate()}
/>
);
}
class Invoice extends React.Component {
render() {
return (
<div
onClick={() => this.props.navigate(`/invoices/1998`, { state: "1998" })}
>
22
</div>
);
}
}
export default withRouter(Invoice);

如有问题,不吝赐教,欢迎留言。

react-router-dom 6.0路由详解的更多相关文章

  1. react router @4 和 vue路由 详解(全)

    react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...

  2. react router @4 和 vue路由 详解(七)react路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...

  3. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

  4. react router @4 和 vue路由 详解(六)vue怎么通过路由传参?

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 8.vue怎么通过路由传参? a.通配符传参数 //在定义路由的时候 { path: ' ...

  5. react router @4 和 vue路由 详解(五)react怎么通过路由传参

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 7.react怎么通过路由传参? a.通配符传参(刷新页面数据不丢失) //在定义路由的 ...

  6. react router @4 和 vue路由 详解(四)vue如何在路由里面定义一个子路由

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 6.vue如何在路由里面定义一个子路由? 给父路由加一个 children:[] 参考我 ...

  7. react router @4 和 vue路由 详解(三)react如何在路由里面定义一个子路由

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 5.react如何在路由里面定义一个子路由?   a.引入在需要子路由的页面引入Rout ...

  8. react router @4 和 vue路由 详解(二)react-router @4用法

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 2.react-router @4用法 a.大概目录      不需要像vue那样麻烦的 ...

  9. react router @4 和 vue路由 详解(一)vue路由基础和使用

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...

  10. Vue 路由详解

    Vue 路由详解 对于前端来说,其实浏览器配合超级连接就很好的实现了路由功能.但是对于单页面应用来说,浏览器和超级连接的跳转方式已经不能适用,所以各大框架纷纷给出了单页面应用的解决路由跳转的方案. V ...

随机推荐

  1. Less-4 报错注入

    补坑:报错注入 当我们 union 无法注入的时候,可以使用报错注入,这里我们有三种报错注入,xpth语法错误和count()+rand()+floor()+group by重复组建错误 extrac ...

  2. 记录一次线上gitlab11.x升级gitlab14.x版本操作

    前言:gitlab11升级到14还是有挺多需要注意的坑,也算是做一次积累吧,升级前后,gitalb的WEB界面也变化了很多,升级过程需要注意的地方我放到最后说明,挺关键的 一.首先需要下载好要升级的包 ...

  3. Python笔记--练习题(都来瞧一瞧,看一看嘞)

    利用Python对文件进行操作 重新写入的文件如下图所示: 统计学生成绩文件的最高分最低分和平均分 Python如何统计英文文章出现最多的单词 Python统计目录下的文件大小 Python按照文件后 ...

  4. App 用户新体验——Agora Native SDK 3.4.0

    声网Agora Native SDK 3.4.0 本月已正式上线.新版本不仅增加了更丰富的实时美声音效.屏幕共享.虚拟节拍器等功能,同时在 SDK 的稳定性.兼容性及安全合规上做了大幅度升级,希望为 ...

  5. MySQL 数据库死锁问题

    在分析案例之前,我们先了解一下MySQL INNODB.在MySQL INNODB引擎中主键是采用聚簇索引的形式,即在B树的叶子节点中既存储了索引值也存储了数据记录,即数据记录和主键索引是存在一起的. ...

  6. 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(12)-Charles如何使用Repeat功能进行简单压力测试

    1.前言 李四:"今天好累啊,点的我手指都疼了.我一直被要求给后端接口的同事重复发送请求来调试接口." Charles:"哎呀,李四同学,你怎么能一条一条的手动发送呢 我 ...

  7. 3.HTTP协议

    HTTP协议 目录 HTTP协议 1.常见HTTP客户端 思考 1.网络协议为什么要分层? 2.www包含了哪些技术? 3.http请求/响应报文包含了哪些内容? 4.http特点有哪些? 2.代理的 ...

  8. C++类的构造函数、析构函数、拷贝构造函数、赋值函数和移动构造函数详细总结

    目录 1. 五种函数介绍 2. 左值&右值怎么区分?怎么看? 3. 匿名对象的3种使用情况 4. 代码详细验证每个函数调用情况 4.1 测试 f_1 函数(函数形参测试 -- 值传递) 4.2 ...

  9. ACM-NEFU新生训练2-排序和CMP

    A.谁考了第k名-排序 Description 在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩. Input 第一行有两个整数,分别是学生的人数n(1≤ ...

  10. '林子雨大数据' 实验3 HBase操作与接口编程

    "林子雨大数据" 实验3 HBase操作与接口编程 环境搭建 VM虚拟机和Ubuntu系统的安装 在Windows中使用VirtualBox安装Ubuntu虚拟机(2020年7月版 ...