从Javascript正确导航到React路由

Correctly Navigating to React Route from Javascript

本文关键字:React 路由 导航 Javascript      更新时间:2023-09-26

我想在使用从post请求返回的数据进行post请求后导航到react路由。使用最新的react路由器,正确的方法是什么?

import {Router} from react-router 
.....
.....
fetch(post_request).then(function(response){
    return response.json()
}).then(function(response){
    this.props.router.push('/new/information/' + response)
})

然而,这给了我一个错误,说路由器没有定义。在这种情况下,我如何在react路由器中正确导航?由于

你需要从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)
  })