2016-06-20 14 views
2

私は、browserHistoryのリスニングメソッドを使用して経路変更をリッスンする反応コンポーネントを持っています。 ComponentDidMountで反応ルータブラウザの削除履歴変更リスナー

browserHistory.listen(this.handleRouteChanged) 

のハンドラを登録した後、componentWillUnmountでリスナーを削除するにはどうすればよいですか?

これについてのドキュメントは見つかりませんでした。githubの問題を検索すると、リスナーを削除しないように思われるbrowserHistory.unregisterTransitionHookを使用して候補が見つかりました。

アイデア?

答えて

4

listen()メソッドは、リッスンを停止する関数を返します。これはhistory docsに文書化されています。

A「の歴史は、」あなたのアプリで異なる画面間のナビゲーションをカプセル化し、そして時に現在の画面の変更をリスナーに通知します。

import { createHistory } from 'history' 

const history = createHistory() 

// Get the current location 
const location = history.getCurrentLocation() 

// Listen for changes to the current location 
const unlisten = history.listen(location => { 
    console.log(location.pathname) 
}) 

// Push a new entry onto the history stack 
history.push({ 
    pathname: '/the/path', 
    search: '?a=query', 

    // Extra location-specific state may be kept in session 
    // storage instead of in the URL query string! 
    state: { the: 'state' } 
}) 

// When you're finished, stop the listener 
unlisten() 
関連する問題