react路由跳转的方式有哪些

这篇文章主要介绍“react路由跳转的方式有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“react路由跳转的方式有哪些”文章能帮助大家解决问题。

成都创新互联公司是专业的沾化网站建设公司,沾化接单;提供成都网站建设、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行沾化网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

方式: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形式,路由跳转后,参数会显示在地址栏

react路由跳转的方式有哪些

  • 跳转的方法是使用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}

    react路由跳转的方式有哪些

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路由跳转的方式有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。

网站题目:react路由跳转的方式有哪些
分享网址:https://www.cdcxhl.com/article34/ieoese.html

成都网站建设公司_创新互联,为您提供软件开发网站设计公司电子商务定制开发品牌网站设计定制网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设