如何在React中获取URL参数?本地主机:3000 /重置密码/:令牌

时间:2019-01-02 12:54:37

标签: reactjs

我已经坚持了一段时间,对React还是很陌生。如何在不使用windows.href的情况下以React的方式获取URL令牌并将其传递给我的resetPasswordToken?

在我的路线上

<Route path="/reset-password/:token" component={ResetToNewPassword} />

URL

http://localhost:3000/reset-password/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiZW1haWxBZGRyZXNzIjoiYXJ0aHVydHJ1b25nLndvcmtAZ21haWwuY29tIiwiZmlyc3ROYW1lIjoiQXJ0aHVyIiwicm9sZSI6IlVzZXIiLCJ0eXBlIjoiUGF0aWVudCIsImxhc3ROYW1lIjoiVHJ1b25nIiwiaWF0IjoxNTQ2NDMyOTA1LCJleHAiOjE1NDY0MzQ3MDV9.LhWsrovx1l4lHDBxZ3nwrMhA_ADoLkZFn2XjKeSzNIg

代码

    handleSubmit = e => {
    e.preventDefault();

    const { onSubmit = () => {} } = this.props;

    this.props.form.validateFields((err, values) => {
      if (err) return;
      const { match = {} } = this.props;
      const { params = {} } = match;
      const resetPasswordToken = params.resetPasswordToken;
      onSubmit({
        ...values,
        resetPasswordToken
      });
    });
  };

1 个答案:

答案 0 :(得分:1)

通过阅读文档,您可以看到您可以从组件访问url参数-在您的情况下为ResetToNewPassword-作为属性match.params

您的情况:match.params.token

此处的示例: https://reacttraining.com/react-router/web/example/url-params