反应路由器 + history.pushState 不工作

react-router + history.pushState not working

我能得到一些关于使用 react-router 正确设置历史库的帮助吗,某处的文档说我必须使用 mixin 才能使用来自 reactrouter 的历史,但其他地方告诉我必须将历史库导入到a完成任务。太混乱了

如果您要更改 react-router 的默认设置,并且仅在设置路由器时才需要导入历史库。否则,你不需要。

无论如何,要使用 history.pushState,您确实需要使用 mixin。如果使用 React Router 1.0.0-rc3,您将执行以下操作(一个简单的示例,但应该明白这一点):

var React   = require('react');
var History = require('react-router').History;

var Link = React.createClass({
  mixins: [ History ],
  _handleClick: function(){
    this.history.pushState(null, "/example-route");  
  },
  render: function() {
    return (
      <div onClick={this._handleClick}>
        Link
      </div>
    );
  },
});

module.exports = Link;