/
eb
/
medium-clone
Обзор
Документация
Войти
/
eb
/
medium-clone
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/node_modules/pages/createArticle/index.js
55 строк
1 KB
Evgeniy Budaev
React Hooks App
25 июл 2020, 15:34
25 июл 2020, 15:34
61784c7
Код
Авторство
О чём код?
import React, {useState, useEffect, useContext} from 'react' import {Redirect} from 'react-router-dom' import ArticleForm from 'components/articleForm' import useFetch from 'hooks/useFetch' import {CurrentUserContext} from 'contexts/currentUser' const CreateArticle = () => { const apiUrl = '/articles' const [isSuccessfullSubmit, setIsSuccessfullSubmit] = useState(false) const [{response, error}, doFetch] = useFetch(apiUrl) const [currentUserState] = useContext(CurrentUserContext) const onSubmit = article => { doFetch({ method: 'post', data: { article } }) } const initialValues = { title: '', description: '', body: '', tagList: '' } useEffect(() => { if (!response) { return } setIsSuccessfullSubmit(true) }, [response]) if (currentUserState.isLoggedIn === null) { return null } if (isSuccessfullSubmit || currentUserState.isLoggedIn === false) { return <Redirect to="/" /> } return ( <div> <ArticleForm onSubmit={onSubmit} initialValues={initialValues} errors={(error && error.errors) || {}} /> </div> ) } export default CreateArticle