您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

react路由跳转的几种方式是什么

2024/11/4 4:08:27发布53次查看
方式:1、利用params,参数会显示在地址栏,语法“...({pathname:...,search:地址栏数据})”;2、利用state,地址栏看不到数据,语法“...({pathname:...,state:{test:...}}”。
本教程操作环境:windows10系统、react17.0.1版、dell g3电脑。
react路由跳转的几种方式是什么注意: 这里使用的react-router-dom是版本5以上,路由形式是history模式
react-router-dom文档地址,其中依赖包history的github地址
1. params形式,路由跳转后,参数会显示在地址栏
跳转的方法是使用history.push({pathname: '/personal', search: 'test=22222'}),其中search键对应的值就是拼接在地址栏的数据import react from 'react'import { usehistory } from 'react-router-dom'export default ()=> { const history = usehistory() // 页面跳转方法 history.push({pathname: '/personal', search: 'test=22222'}) return 123}
接收的方法。数据都是存储在uselocation中的search获取import react from 'react'import { uselocation } from 'react-router-dom'export default ()=> { const location = uselocation() // 页面跳转方法 console.log(location, 'props') return 123}

2. 使用state的形式,页面刷新不会丢失数据,并且地址栏也看不到数据跳转的方法是使用history.push({pathname: '/personal', state: {test: 'dashboard'}}),其中search键对应的值就是拼接在地址栏的数据import react from 'react'import { usehistory } from 'react-router-dom'export default ()=> { const history = usehistory() // 页面跳转方法 history.push({pathname: '/personal', state: { test: 'dashboard' }}) return 123}
接收的方法。数据都是存储在uselocation中的search获取import react from 'react'import { uselocation } from 'react-router-dom'export default ()=> { const location = uselocation() // 页面跳转方法 console.log(location, 'props') return 123}

推荐学习:《react视频教程》
以上就是react路由跳转的几种方式是什么的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product