/
eb
/
medium-clone
Обзор
Документация
Войти
/
eb
/
medium-clone
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/node_modules/components/currentUserChecker.js
32 строки
772 B
Evgeniy Budaev
React Hooks App
25 июл 2020, 15:34
25 июл 2020, 15:34
61784c7
Код
Авторство
О чём код?
import {useEffect, useContext} from 'react' import useFetch from 'hooks/useFetch' import {CurrentUserContext} from 'contexts/currentUser' import useLocalStorage from 'hooks/useLocalStorage' const CurrentUserChecker = ({children}) => { const [, dispatch] = useContext(CurrentUserContext) const [{response}, doFetch] = useFetch('/user') const [token] = useLocalStorage('token') useEffect(() => { if (!token) { dispatch({type: 'SET_UNAUTHORIZED'}) return } doFetch() dispatch({type: 'LOADING'}) }, [doFetch, dispatch, token]) useEffect(() => { if (!response) { return } dispatch({type: 'SET_AUTHORIZED', payload: response.user}) }, [response, dispatch]) return children } export default CurrentUserChecker