簡體   English   中英

React.js如何從功能組件訪問路由器鏈接URL中的參數?

[英]React.js how to access a parameter in a Router Link URL from a function Component?

我可以使用以下命令從路由器URL訪問參數:

${this.props.match.params.id}

有沒有一種方法可以從功能組件而不是類組件訪問相同的參數?

您可以使用react-router提供withRouter hoc

您可以通過withRouter高階組件訪問歷史對象的屬性和最接近的匹配項。 每當呈現時,withRouter都會將更新的匹配,位置和歷史道具傳遞給包裝的組件。

import { withRouter } from 'react-router';

const Component = props => ...

// now you can access props.history|match|location
const ComponentWithRouteProps = withRouter(Component);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM