0

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

1 Answer 1

1

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)
  })
Sign up to request clarification or add additional context in comments.

3 Comments

this gives me an error context is undefined. how can I fix this? Thanks
Which version of react-router you use and if you use version >2.0.0 then did you define the contextTypes in your component
Hi, the code is working now with some small modifications however the browser URL is not changing. I'm using the latest version of react, new question here please check stackoverflow.com/questions/39973549/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.