網站首頁 編程語言 正文
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);
}
原文鏈接:https://blog.csdn.net/weixin_43972992/article/details/120749861
相關推薦
- 2022-10-01 Go編譯原理之函數內聯_Golang
- 2023-05-07 GO中什么情況會使用變量逃逸_Golang
- 2022-05-16 解析Sentry?Relay?二次開發調試_python
- 2022-08-18 C/C++中的new和delete的實現過程小結_C 語言
- 2022-05-31 詳解Flutter如何繪制曲線,折線圖及波浪動效_Android
- 2022-08-13 解決Artifact spbjh:war exploded: Error during artifa
- 2022-12-26 .NET?6實現滑動驗證碼的示例詳解_實用技巧
- 2022-05-22 SQL?Server數據庫基本概念、組成、常用對象與約束_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支