2015-10-15 116 views
1

我可以通过react-router获得一些帮助,正确设置历史库,文档某处说我必须使用mixin从reactrouter使用历史记录,但是别的地方告诉我说我必须导入历史库才能完成任务。这是如此令人困惑react-router + history.pushState不工作

回答

3

如果您要更改react-router的默认设置并且仅用于安装路由器时,您只需导入历史记录库。否则,你不需要。

无论如何,要使用history.pushState,您确实需要使用mixin。如果使用路由器做出反应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;

+1

感谢!!!!!! :) –

+0

你能告诉区别this.history.pushState(null,“/ example-route”);和this.history.pushState(null,“example-route”); ..我从url的开头删除了正斜杠... –

+0

我不知道它是否重要! – Chiedo