且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 React 从 url 获取参数?

更新时间:2023-02-23 21:54:17

您可以使用 withRouter 获取路由器信息,例如位置、历史记录、路径和参数.

You can use withRouter for getting router information such as location, history, path, and params.

import { withRouter } from "react-router-dom";

...

const VerifyAccount = withRouter(props) => {
  const { token, email } = props.match.params;
  console.log("toke, email: ", token, email)     // And also show me how it looks!!!
  ...
}

你应该像这样定义你的路线.

And you should define your route like this.

<Route path='/verify-account/:token/:email' component={VerifyAccountPage} />

你应该这样调用:

https://your-domain/verify-account/[token]/[email]

这会起作用.