React Router 3, exactly match against query param

2019-07-13 07:30发布

In React Router 3, how do you match exactly against a route containing query params?

Something like this,

<Route path="about?qs1=:qs&qs2=:qs2" component={About} />

1条回答
Viruses.
2楼-- · 2019-07-13 07:48

Query params are not part of a route in that sense. You will need to check for them inside your component, for example like this:

class About extends React.Component {
  render() {
    return(
      <div>
        {this.props.location.query.qs1 ? 'Correct route!' : 'Invalid route!'}
      </div>
    );
  }
}

You could also check for the query params inside componentDidMount and redirect your users to a different route (e.g. 404). Read more about Route Matching in the official docs.

查看更多
登录 后发表回答