Skip to content

Instantly share code, notes, and snippets.

@riskers
Created June 20, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riskers/2db58797e85dc25f0d8a77ce91440a5b to your computer and use it in GitHub Desktop.
Save riskers/2db58797e85dc25f0d8a77ce91440a5b to your computer and use it in GitHub Desktop.
react router 获取 query params
<Router>
  <div className="router-container">
    <Route exact path="/" component={ Home } />
    <Route exact path="/search" component={ Search } />
  </div>
</Router>

比如现在搜索要搜 t: texts: abc,路由应该是 /search?t=text&s=abc

react-router 在 v4 之后不能获取 query params 了,所以在 Search 组件里 queryString.parse(this.props.location.search) 就行。

queryString 是 react-router 推荐使用的模块: npm install --save query-string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment