This custom hook allows you to make generic API calls in your React application with ease. The hook abstracts the common logic for making API requests, so you can focus on building your application's unique features.
For a detailed explanation on how to use this custom hook, check out the article on Medium: React Generic API Calls with Custom Hooks
To use this custom hook, simply import it into your React component and pass in the necessary parameters for your API call (e.g., URL, request method, request body, etc.). The hook will return the response data, loading state, and any errors that may have occurred during the request.
Here's a basic example of how to use the custom hook in a component:
import React from "react";
import {useGet} from "react-generic-api-call-hooks";
const MyComponent = () => {
const {getData, data , isLoading, error} = useGet()
useEffect(()=>{
getData('url','token)
},[])
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
<h1>Data from API:</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
};
export default MyComponent;