詳解React路由傳參方法匯總記錄
React中傳參方式有很多,通過路由傳參的方式也是必不可少的一種。
本文記錄項目中會用到的路由傳參方式: 路由跳轉(zhuǎn)傳參API + 目標路由獲取參數(shù)的方式。
一、動態(tài)路由
跳轉(zhuǎn)方法
Link
<Link to={{ pathname: "/user/add/1" }}>跳轉(zhuǎn)新增頁面</Link>
history.push
this.props.history.push("/user/add/1");
獲參方法
this.props.match.params
二、路由query顯示參數(shù)
Link跳轉(zhuǎn)
<Link to={{ pathname: "/user/add", query: { id: 1 } }}>
跳轉(zhuǎn)新增頁面
</Link>
history.push
this.props.history.push({
pathname: "/user/add",
//參數(shù)
query: { id: 1 },
});
獲參方法
this.props.location.query
三、路由state隱式參數(shù)
Link跳轉(zhuǎn)
<Link to={{ pathname: "/user/add", state: { id: 1 } }}>
跳轉(zhuǎn)新增頁面
</Link>
history.push
this.props.history.push({
pathname: "/user/add",
state: { id: 1 },
});
獲參方法
this.props.location.state
代碼DEMO
入口App組件
class App extends React.Component {
render() {
return (
<BrowserRouter>
<Route path="/user" exact component={User} />
//動態(tài)路由
<Route path="/user/add/:id?" component={UserAdd} />
{/* <Redirect to="/user" /> */}
</BrowserRouter>
);
}
}
動態(tài)路由Demo
User組件
import React, { Component } from "react";
import { Link } from "react-router-dom";
export default class User extends Component {
render() {
return (
<div>
<div className="user">User</div>
{/* <Link to={{ pathname: "/user/add/1" }}>跳轉(zhuǎn)新增頁面</Link> */}
<button
onClick={() => {
// 1.
// this.props.history.push("/user/add/1");
// 2.
this.props.history.push({
pathname: "/user/add/1",
});
}}
>
跳轉(zhuǎn)新增頁面
</button>
</div>
);
}
}
UserAdd組件
import React, { Component } from "react";
export default class UserAdd extends Component {
render() {
console.log("this.props.match.params:", this.props.match.params);
return <div>UserAdd</div>;
}
}
路由query顯示參數(shù)Demo
User組件
import React, { Component } from "react";
import { Link } from "react-router-dom";
export default class User extends Component {
render() {
return (
<div>
<div className="user">User</div>
<Link to={{ pathname: "/user/add", query: { id: 1 } }}>
跳轉(zhuǎn)新增頁面
</Link>
<button
onClick={() => {
this.props.history.push({
pathname: "/user/add",
query: { id: 1 },
});
}}
>
跳轉(zhuǎn)新增頁面
</button>
</div>
);
}
}
路由state隱式參數(shù)Demo
User組件
import React, { Component } from "react";
import { Link } from "react-router-dom";
export default class User extends Component {
render() {
return (
<div>
<div className="user">User</div>
<Link to={{ pathname: "/user/add", state: { id: 1 } }}>
跳轉(zhuǎn)新增頁面
</Link>
<button
onClick={() => {
this.props.history.push({
pathname: "/user/add",
state: { id: 1 },
});
}}
>
跳轉(zhuǎn)新增頁面
</button>
</div>
);
}
}

到此這篇關(guān)于詳解React路由傳參方法匯總記錄的文章就介紹到這了,更多相關(guān)React路由傳參內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React?數(shù)據(jù)共享useContext的實現(xiàn)
本文主要介紹了React?數(shù)據(jù)共享useContext的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
React不使用requestIdleCallback實現(xiàn)調(diào)度原理解析
這篇文章主要為大家介紹了React不使用requestIdleCallback實現(xiàn)調(diào)度原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
React在Dva項目中創(chuàng)建并引用頁面局部組件的方式
這篇文章主要介紹了React在Dva項目中創(chuàng)建并引用頁面局部組件的方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
React?hook實現(xiàn)簡單的websocket封裝方式
這篇文章主要介紹了React?hook實現(xiàn)簡單的websocket封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

