rksh
rksh

Reputation: 4050

Correctly Navigating to React Route from Javascript

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

Upvotes: 0

Views: 60

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281626

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)
  })

Upvotes: 1

Related Questions