2016-11-09 1 views
5

내 응용 프로그램의 탐색을 해당 상태에서 실행하려고합니다. 반응 라우터에서 현재 위치를 얻는 방법은 무엇입니까?

Given the user is on the Login page 
When the application state changes to isLoggedIn 
Then the application should navigate to the Dashboard page 

가 여기에 작동 나의 현재의 구현입니다 : 여기 내 유스 케이스의
if (browserHistory.getCurrentLocation().pathname === '/login' && store.isLoggedIn) { 
    browserHistory.push('/dashboard'); 
} 

불행하게도 내가 browserHistory.getCurrentLocation() 공식 API 아니라는 것을 이해하고, 반응-라우터 문서와 문제를 겪고 그것은 안 익숙한. 이 작업을 수행 할 수있는 권장 방법이 있습니까?

P. 이 코드는 React 구성 요소 외부에서 실행되므로 사용할 수 없습니다. this.props.location.pathname

답변

0
export const onAuthChange = (isAuthenticated) => { 
    const pathname = browserHistory.getCurrentLocation().pathname; 
    const isUnauthenticatedPage = unauthenticatedPages.includes(pathname); 
    const isAuthenticatedPage = authenticatedPages.includes(pathname); 

    if (isUnauthenticatedPage && isAuthenticated) { 
    browserHistory.replace('/Dashboard'); 
    } else if (isAuthenticatedPage && !isAuthenticated) { 
    browserHistory.replace('/'); 
    } 

}; 
+2

답변을 설명하면 다른 사용자에게 유용 할 때가 있습니다. 미래에는 예상치 못한 방식으로 다른 사람들을 도울 수 있지만 코드 블록 일 경우 그럴 가능성이 적습니다. – Brody

관련 문제