基于React路由跳轉(zhuǎn)的幾種方式
React路由跳轉(zhuǎn)的幾種方式
注意: 這里使用的react-router-dom是版本5以上,路由形式是history模式
react-router-dom文檔,其中依賴包history的github地址
1. params形式
路由跳轉(zhuǎn)后,參數(shù)會顯示在地址欄

跳轉(zhuǎn)的方法是使用
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()
// 頁面跳轉(zhuǎn)方法
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()
// 頁面跳轉(zhuǎn)方法
console.log(location, 'props')
return 123
}
2. 使用state的形式
頁面刷新不會丟失數(shù)據(jù),并且地址欄也看不到數(shù)據(jù) 跳轉(zhuǎn)的方法是使用
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()
// 頁面跳轉(zhuǎn)方法
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()
// 頁面跳轉(zhuǎn)方法
console.log(location, 'props')
return 123
}
React路由跳轉(zhuǎn)傳參問題
使用Link傳參
1.引入Link
import { Link } from “react-router-dom”2.Llink上攜帶傳遞的參數(shù),攜帶的參數(shù)以對象形式
<Link to={
?? ??? ??? ?{ pathname: "/XXX", //xxx為跳轉(zhuǎn)到的路徑
?? ??? ??? ? ?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', //要跳轉(zhuǎn)到的路徑
? ? 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', //要跳轉(zhuǎn)到的路徑
? ? query: { ?
? ? ? id: 456
? ? }
? })}}>去詳情頁</button>接收參數(shù)
<Route exact path="/detail" component={Detail}></Route>
//頁面接收參數(shù)
componentDidMount(){
? ? console.log(this.props.history.location.query);
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- React進行路由跳轉(zhuǎn)的方法匯總
- React路由跳轉(zhuǎn)的實現(xiàn)示例
- React 中常用的幾種路由跳轉(zhuǎn)方式小結(jié)
- react路由跳轉(zhuǎn)傳參刷新頁面后參數(shù)丟失的解決
- react中路由跳轉(zhuǎn)及傳參的實現(xiàn)
- React中的Hooks路由跳轉(zhuǎn)問題
- React中的路由嵌套和手動實現(xiàn)路由跳轉(zhuǎn)的方式詳解
- React-RouterV6+AntdV4實現(xiàn)Menu菜單路由跳轉(zhuǎn)的方法
- react-router v4如何使用history控制路由跳轉(zhuǎn)詳解
- react 路由跳轉(zhuǎn)的7種方式實現(xiàn)
相關文章
React父組件數(shù)據(jù)實時更新了,子組件沒有更新的問題
這篇文章主要介紹了React父組件數(shù)據(jù)實時更新了,子組件沒有更新的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
React配置Redux并結(jié)合本地存儲設置token方式
這篇文章主要介紹了React配置Redux并結(jié)合本地存儲設置token方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
React函數(shù)式組件Hook中的useEffect函數(shù)的詳細解析
useEffect是react v16.8新引入的特性。我們可以把useEffect hook看作是componentDidMount、componentDidUpdate、componentWillUnmounrt三個函數(shù)的組合2022-10-10
IntersectionObserver實現(xiàn)加載更多組件demo
這篇文章主要為大家介紹了IntersectionObserver實現(xiàn)加載更多組件demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

