2016-04-18 13 views
1

私はパス/helloに移動して、次のことを試してみたい:React Router:コンポーネントの外部でプログラム的にナビゲートする方法は?

import { browserHistory } from 'react-router' 
browserHistory.push('/hello') 

しかし、それだけで、実際にそのページに移動することなく/helloにブラウザのURLを変更します。コンポーネントの内部では、この方法では動作します:

​​

参考文献:

  1. https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#programmatic-navigation
  2. https://github.com/reactjs/react-router/issues/1081

答えて

1

this.props.history.push('/hello') 

これは私が私のルータを設定する方法です

IセットアップbrowserHistoryは、私はグローバルネームスペース内部に格納:

const history = syncHistoryWithStore(browserHistory, store, { 
    selectLocationState: (state) => state.router 
}) 
window.routerHistory = history 

そしてIは以下のような構成要素の外側に移動することができ:

window.routerHistory.push('/') 
関連する問題