網站首頁 編程語言 正文
React路由跳轉的幾種方式
注意: 這里使用的react-router-dom是版本5以上,路由形式是history模式
react-router-dom文檔,其中依賴包history的github地址
1. params形式
路由跳轉后,參數(shù)會顯示在地址欄
跳轉的方法是使用
history.push({pathname: '/personal', search: 'test=22222'})
其中search鍵對應的值就是拼接在地址欄的數(shù)據(jù)
import React from 'react'
import { useHistory } from 'react-router-dom'
export default ()=> {
const history = useHistory()
// 頁面跳轉方法
history.push({pathname: '/personal', search: 'test=22222'})
return 123
}
接收的方法。數(shù)據(jù)都是存儲在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的形式
頁面刷新不會丟失數(shù)據(jù),并且地址欄也看不到數(shù)據(jù) 跳轉的方法是使用
history.push({pathname: '/personal', state: {test: 'dashboard'}})
其中search鍵對應的值就是拼接在地址欄的數(shù)據(jù)
import React from 'react'
import { useHistory } from 'react-router-dom'
export default ()=> {
const history = useHistory()
// 頁面跳轉方法
history.push({pathname: '/personal', state: { test: 'dashboard' }})
return 123
}
接收的方法。數(shù)據(jù)都是存儲在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上攜帶傳遞的參數(shù),攜帶的參數(shù)以對象形式
<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 接收參數(shù)(類組件)
componentDidMount() {
? ? ? ? console.log(this.props.location.state.XXX); ? //xxx指state對象中的鍵名 ? ?
? ? }
2.2接收參數(shù)(函數(shù)式組件)
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.接收參數(shù)
// ?react-router-dom是通過“/:”去匹配url傳遞的參數(shù) ,即id獲取到上面的url傳過來的88
<Route exact path="/detail/:id" component={Detail}></Route>
//頁面接收參數(shù)
componentDidMount(){
? console.log(this.props.match.params);
}
隱式傳參
3.1 state方法
頁面?zhèn)鲄?/p>
state方法傳參:參數(shù)地址欄不顯示,刷新地址欄,參數(shù)不丟失
<button onClick={()=>{this.props.history.push({
? ? pathname: '/detail', //要跳轉到的路徑
? ? state: { ?//參數(shù)地址欄不顯示,刷新地址欄,參數(shù)不丟失
? ? ? id: 456
? ? }
? })}}>去詳情頁</button>
接收參數(shù)
<Route exact path="/detail" component={Detail}></Route>
//頁面接收參數(shù)
componentDidMount(){
? ? console.log(this.props.history.location.state);
}
3.2 query方法
頁面?zhèn)鲄?/p>
query方法傳參:參數(shù)地址欄不顯示,刷新地址欄,參數(shù)丟失
<button onClick={()=>{this.props.history.push({
? ? pathname: '/detail', //要跳轉到的路徑
? ? query: { ?
? ? ? id: 456
? ? }
? })}}>去詳情頁</button>
接收參數(shù)
<Route exact path="/detail" component={Detail}></Route>
//頁面接收參數(shù)
componentDidMount(){
? ? console.log(this.props.history.location.query);
}
原文鏈接:https://blog.csdn.net/weixin_43972992/article/details/120749861
相關推薦
- 2022-12-21 C和C++中argc和argv的含義及用法詳解_C 語言
- 2022-02-27 Error in render: “TypeError: Cannot read propertie
- 2023-10-13 Error: cannot push because a reference that you ar
- 2023-07-27 call、apply、bind三者的用法和區(qū)別
- 2022-04-15 C語言的程序環(huán)境與預處理你真的了解嗎_C 語言
- 2022-06-02 了解Kubernetes中的Service和Endpoint_云和虛擬化
- 2022-08-13 IDEA快速展開和折疊方法代碼快捷鍵
- 2022-08-21 caffe的python接口生成配置文件學習_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支