Last active
March 9, 2020 19:30
-
-
Save leandrojo/e13e532c44500f69afbc5dad100f0664 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-disable react-hooks/exhaustive-deps */ | |
| import { useState } from 'react'; | |
| const useRequest = (request: Function = () => ({})) => { | |
| const [data, setData] = useState(null); | |
| const [isError, setIsError] = useState(false); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const onSubmit = async (body) => { | |
| setIsError(false); | |
| setIsLoading(true); | |
| try { | |
| const response = await request(body); | |
| setData(response); | |
| } catch (error) { | |
| setIsError(true); | |
| setData(error); | |
| } | |
| setIsLoading(false); | |
| }; | |
| return { | |
| data, isError, isLoading, onSubmit, | |
| }; | |
| }; | |
| export default useRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment