How to get the current location from a responsive router?

I am trying to control my application navigation from my state. Here is my use case:

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

Here is my current implementation that works:

 if (browserHistory.getCurrentLocation().pathname === '/login' && store.isLoggedIn) { browserHistory.push('/dashboard'); } 

Unfortunately, going through documents and problems with reactor routers, I understand that browserHistory.getCurrentLocation() not an official API and should not be used. Is there a recommended way to do this?

PS This code works outside of the React component, so I cannot use this.props.location.pathname

+5
source share
2 answers
 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('/'); } }; 
0
source

Javascript has a built-in window.location method, heres the docs: http://www.w3schools.com/js/js_window_location.asp . I think this will be a good solution for what you are trying to accomplish.

-2
source

Source: https://habr.com/ru/post/1259514/


All Articles