React Router不保持查询

时间:2018-04-03 11:35:08

标签: reactjs router

我发送的查询包含以下链接:

 <Link to={{ 
     pathname: `/forum/${this.state.mainCategoryName}/${category.name}`, 
     query: { subcatid: category.id} }
   }
 >
  {category.name}
 </Link>

这工作正常,我可以像这样呈现值:

 this.props.location.query.subcatid

但是,只要我刷新页面或以某种方式以另一种方式进入页面(使用链接除外),查询就不会被包含。

我怎样才能做到这一点?

我的路由器看起来像这样:

<Route 
  exact={true} 
  path="/forum/:forumcatid/:forumsubcatid" 
  render={
    (routeProps) => ( 
      <ForumThreads {...routeProps} {...this.props} /> 
    )
  } 
/>

1 个答案:

答案 0 :(得分:0)

也许要与ComponentDidMount内的ForumThreads核实一下。在完整页面刷新之后,你没有pass查询,它就在那里。

ComponentDidMount(){
 // here check
 // this.props.location.search 
 // to see if it allready contains some query strings inside url 
 // and then do your magic :)
}