2015-10-15 113 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