reactjs – 如何从react-router获取当前位置?
发布时间:2020-12-15 20:18:58 所属栏目:百科 来源:网络整理
导读:我试图从其状态驱动我的应用程序的导航.这是我的用例: Given the user is on the Login pageWhen the application state changes to isLoggedInThen the application should navigate to the Dashboard page 这是我当前的实现有效: if (browserHistory.get
我试图从其状态驱动我的应用程序的导航.这是我的用例:
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'); } 不幸的是,通过react-router文档和问题,我知道browserHistory.getCurrentLocation()不是官方API,不应该使用它.有推荐的方法吗? 附:此代码在React组件外部运行,因此我不能使用this.props.location.pathname 解决方法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('/'); } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |