import React from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import useStoreSelector from 'hooks/store/useStoreSelector'
function useRedirectToInitialPage(): void {
const userState = useStoreSelector((store) => store.userState)
const location = useLocation()
const navigate = useNavigate()
React.useEffect(() => {
if (userState.isAuthorized && location.state?.from) {
navigate(location.state?.from)
}
}, [userState.isAuthorized]) // eslint-disable-line react-hooks/exhaustive-deps
}
export default useRedirectToInitialPage
import React from 'react'
import Layout from 'components/layout/Layout/Layout'
import AuthManager from 'utils/components/managers/AuthManager'
import PageManager from 'utils/components/managers/PageManager'
import useRedirectToInitialPage from 'hooks/auth/useRedirectToInitialPage'
const App: React.FunctionComponent = () => {
useRedirectToInitialPage()
return (
<Layout>
<AuthManager />
<PageManager />
</Layout>
)
}
export default App
import React from 'react'
import Loader from 'components/common/feedback/Loader/Loader'
import Window from 'components/common/feedback/Window/Window'
import useSubscribeToAuthChange from 'hooks/auth/useSubscribeToAuthChange'
// import useRedirectToInitialPage from 'hooks/auth/useRedirectToInitialPage'
const AuthManager: React.FunctionComponent = () => {
const subscribing = useSubscribeToAuthChange()
// useRedirectToInitialPage()
return (
<>
<Loader
visible={subscribing.isLoading}
details="Инициализация"
/>
<Window
visible={subscribing.isError}
variant="message"
heading="Ошибка"
details={subscribing.error?.message ?? ''}
onClose={subscribing.reset}
/>
</>
)
}
export default AuthManager
Layout
никак не взаимодействует с redux
и просто формирует несколько вложенных div
-элементов.useRedirectToInitialPage
внутри компонента AuthManager
, функция navigate
не срабатывает, хотя userState.isAuthorized
равен true
и location.state?.from
равен исходному адресу.