2016-06-20 4 views
2

browserHistory의 수신 방법을 사용하여 경로 변경을 수신하는 반응 구성 요소를 사용했습니다. ComponentDidMount에서반응 라우터 브라우저 제거하기 히스토리 변경 수신기

browserHistory.listen(this.handleRouteChanged) 

와 핸들러를 등록한 후, 어떻게 componentWillUnmount에서 리스너를 제거하는 방법은 무엇입니까?

이것에 대한 문서를 찾을 수 없으며 github 문제를 검색 할 때 listener를 제거하지 않는 것으로 보이는 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() 
관련 문제