使用Js控制ReactRouter路由

Posted Tekkaman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Js控制ReactRouter路由相关的知识,希望对你有一定的参考价值。

使用Js控制ReactRouter路由

  首先引入PropTypes:

const PropTypes = require(‘prop-types‘);

  然后定义context的router属性:

ShoppingCart.contextTypes = {
  router: PropTypes.object
};

  最后,使用代码跳转即可。

this.context.router.history.replace(‘/login‘)

   goBack方法可以用于回退到前一页面。

this.context.router.history.goBack()

 

参考:

1、https://facebook.github.io/react/docs/context.html

2、http://blog.csdn.net/u010977147/article/details/53490063(此博文面向的是老版本ReactRouter)

以上是关于使用Js控制ReactRouter路由的主要内容,如果未能解决你的问题,请参考以下文章