Skip to content

Instantly share code, notes, and snippets.

@leandrojo
Last active March 9, 2020 19:30
Show Gist options
  • Select an option

  • Save leandrojo/e13e532c44500f69afbc5dad100f0664 to your computer and use it in GitHub Desktop.

Select an option

Save leandrojo/e13e532c44500f69afbc5dad100f0664 to your computer and use it in GitHub Desktop.
/* 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