简体   繁体   English

具有参数的匹配路由不适用于react-router v4

[英]match route that has param doesn't work with react-router v4

<Route path='/change-password/?resetToken=(:token)' component={()=><h1>testing</h1>} />

Above route don't render when I hit the url below? 当我点击下面的网址时,上面的路线没有显示?

http://localhost:3000/change-password/?resetToken=123

I also tried path='/change-password/?resetToken=:token' 我也尝试过path='/change-password/?resetToken=:token'

So the main problem seems to be with the question mark in the path . 因此,主要问题似乎与path问号有关。 But first you need to write :token instead of (:token) , here is an example format of path with params on github docs of the react-router . 但是首先您需要编写:token而不是(:token)react-router github文档上带params的path示例格式。

I followed this github post about no way to set reserved characters in the path name, but it didn't lead me to anything. 我遵循了这个 github帖子,内容是关于如何在路径名中设置保留字符的方法,但这并没有带我任何东西。 One way you could solve the problem is to define your route like /change-password and then in the actual component do this.props.location.search.split("=")[1] to get the value of the token from the search query. 解决问题的一种方法是定义路由,例如/change-password ,然后在实际组件中执行此操作this.props.location.search.split("=")[1]来从令牌中获取令牌的值搜索查询。 Here is an example: 这是一个例子:

class ChangePassword extends React.Component {
  componentDidMount() {
    // get the token, check if it exists and do smth with it if it does
    console.log(this.props.location.search.split("=")[1]);
  }
  render() {
    return (<h1>Change password</h1>);
  };
}
const App = () => (
  <Router>
    <div>
      <Route path='/change-password' component={ChangePassword} />
      <Route path='/home' component={ChangePassword} />
    </div>
  </Router>
);

It seems that react-router doesn't handle ? 看来react-router无法处理? (or other reserved characters, haven't tested) in the path name, or I am seriously missing out something here and there is some magic option that makes it work :) I didn't find one but I made a working example with the code I provided on codesanbdbox with your path in the Route . (或其他保留字符,未经测试)在路径名中,或者我在这里严重遗漏了一些东西,并且有一些神奇的选项可以使它起作用:)我没有找到一个,但是我用我在codesanbdbox上提供的代码以及您在Routepath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM