问题描述:

I want to navigate to a react route after making a post request using the data returned from the post request. What's the correct way to do it using the latest react router?

import {Router} from react-router

.....

.....

fetch(post_request).then(function(response){

return response.json()

}).then(function(response){

this.props.router.push('/new/information/' + response)

})

However this gives me an error saying router is not defined. How can I correctly navigate in react router in this situation? Thanks

网友答案:

You need to call router from context

  contextTypes: {
    router: React.PropTypes.object.isRequired
  }
  fetch(post_request).then(function(response){
      return response.json()
  }).then(function(response){
      this.context.router.push('/new/information/' + response)
  })
相关阅读:
Top