如何在react-router中发送查询

时间:2016-10-09 14:04:46

标签: reactjs react-router-redux

handleClick2(e, item, id) {
  e.stopPropagation()
  browserHistory.push({
    pathname: `/detail/${item}/${id}`,
    query: {crx:'handsome'},
    state: 'daf'
  })
}

如何以browserHistory(react-router)的方式发送查询?

2 个答案:

答案 0 :(得分:0)

Try this

browserHistory.push({
        pathname:`/detail/${item}/${id}?crx=handsome`,
        state:'daf'
    })

答案 1 :(得分:0)

如果您使用的是react-router v1.0.x,则应使用this.context.history.pushState(),如果您使用的是>= v2.0.0,请使用this.context.router.push()。这是你如何使用它

<强> this.context.history.pushState(...)

// v1.0.x
    const SomeComponent = React.createClass({
      contextTypes: {
        history: React.PropTypes.object.isRequired
      },
      handleClick2(e,item,id){

       e.stopPropagation()
          this.context.history.pushState('daf', '`/detail/${item}/${id}`', {crx:'handsome'})

      }
    })

<强> this.context.router.push()

   // >= v2.0.0
    const SomeComponent = React.createClass({
      contextTypes: {
        router: React.PropTypes.object.isRequired
      },
      handleClick2(e,item,id){

       e.stopPropagation()
          this.context.router.push({
             pathname:`/detail/${item}/${id}`,
             query:{crx:'handsome'},
             state:'daf'
          })

      }
    })