基于React路由跳转的几种方式
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
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路由跳转传参问题
使用Link传参
1.引入Link
import { Link } from “react-router-dom”
2.Llink上携带传递的参数,携带的参数以对象形式
<Link to={ ?? ??? ??? ?{ pathname: "/XXX", //xxx为跳转到的路径 ?? ??? ??? ? ?state: { title: this.state.exam.title, actionCode: this.state.exam.actionCode }? ?? ??? ??? ?} ?? ??? ? ?} >切换考试科目 <i className="iconfont icon-jiantou"></i></Link>
2.1 接收参数(类组件)
componentDidMount() { ? ? ? ? console.log(this.props.location.state.XXX); ? //xxx指state对象中的键名 ? ? ? ? }
2.2接收参数(函数式组件)
function Fast(props) { ? ? ?... ? ? useEffect(() => { ? ? ? ? console.log(props.location.state.XXX);//xxx指state对象中的键名 ? ? }) }
url传参
1.url带参传参
<button onClick={()=>{this.props.history.push('/detail/88')}}>跳往详情页</button>`
2.接收参数
// ?react-router-dom是通过“/:”去匹配url传递的参数 ,即id获取到上面的url传过来的88 <Route exact path="/detail/:id" component={Detail}></Route> //页面接收参数 componentDidMount(){ ? console.log(this.props.match.params); }
隐式传参
3.1 state方法
页面传参
state方法传参:参数地址栏不显示,刷新地址栏,参数不丢失
<button onClick={()=>{this.props.history.push({ ? ? pathname: '/detail', //要跳转到的路径 ? ? state: { ?//参数地址栏不显示,刷新地址栏,参数不丢失 ? ? ? id: 456 ? ? } ? })}}>去详情页</button>
接收参数
<Route exact path="/detail" component={Detail}></Route> //页面接收参数 componentDidMount(){ ? ? console.log(this.props.history.location.state); }
3.2 query方法
页面传参
query方法传参:参数地址栏不显示,刷新地址栏,参数丢失
<button onClick={()=>{this.props.history.push({ ? ? pathname: '/detail', //要跳转到的路径 ? ? query: { ? ? ? ? id: 456 ? ? } ? })}}>去详情页</button>
接收参数
<Route exact path="/detail" component={Detail}></Route> //页面接收参数 componentDidMount(){ ? ? console.log(this.props.history.location.query); }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持程序员之家。
相关文章
React render核心阶段深入探究穿插scheduler与reconciler
这篇文章主要介绍了React render核心阶段穿插scheduler与reconciler,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧2022-11-11使用 Rails API 构建一个 React 应用程序的详细步骤
这篇文章主要介绍了使用 Rails API 构建一个 React 应用程序的详细步骤,主要包括后端:Rails API部分,前端:React部分及React组件的相关操作,具有内容详情跟随小编一起看看吧2021-08-08React+TypeScript+webpack4多入口配置详解
这篇文章主要介绍了React+TypeScript+webpack4多入口配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08React+TS+IntersectionObserver实现视频懒加载和自动播放功能
通过本文的介绍,我们学习了如何使用 React + TypeScript 和 IntersectionObserver API 来实现一个视频播放控制组件,该组件具有懒加载功能,只有在用户滚动页面且视频进入视口时才开始下载视频资源,需要的朋友可以参考下2023-04-04Taro?React自定义TabBar使用useContext解决底部选中异常
这篇文章主要为大家介绍了Taro?React底部自定义TabBar使用React?useContext解决底部选中异常(需要点两次才能选中的问题)示例详解,有需要的朋友可以借鉴参考下2023-08-08
最新评论