react-router match query string

橙三吉。 提交于 2020-01-03 16:49:41

问题


https://github.com/reactjs/react-router/example

I tried the example on npm start

query string match to route is not working on the example. When I click the second one, It's activating wrong one

http://localhost:8080/query-params/user/bob?showAge=1 and refreshing on the link above not matching any routes.

Even if I have change the example code to below <Route path="user/:userID(?:showAge)" component={User} /> I tried couple of things that might work based on docs but none of them worked.

Am I missing something?


回答1:


Turned out there was error on the example on react-router github.

I've made a PR for it which removes activeClassName for Link component.

without it, it works fine, and query string is in location props, see below

this.props.location.query = { 
  query1: value1,
  query2: value2
}

this picture




回答2:


You are missing the root (/) in path="user/:userID(?:showAge)"

This code must works:

 <Router history={history}>
    <Route path="/user/:userID/:showAge)" component={User} />
 </Router>

Check if the params are in the route:

  console.log(JSON.stringify(this.props.routeParams));

My whole file



来源:https://stackoverflow.com/questions/36094765/react-router-match-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!