A collection of custom React hooks for fetching data from APIs.
To install the package, run the following command:
npm install @rohitnirban/apihooks
A custom hook for making GET requests to an API endpoint.
import useGetApi from '@rohitnirban/apihooks';
const { isLoading, error, data } = useGetApi('/api/endpoint');
A custom hook for making POST requests to an API endpoint.
import usePostApi from '@rohitnirban/apihooks';
const { isLoading, error, data, setData } = usePostApi('/api/endpoint');
const { isLoading, error, data } = useGetApi('/api/get');
if (isLoading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Error: {error}</p>;
}
return <p>Data: {data}</p>;
const { isLoading, error, data, setData } = usePostApi('/api/post');
const handleSubmit = (formData) => {
setData(formData);
};
if (isLoading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Error: {error}</p>;
}
return (
<form onSubmit={handleSubmit}>
{/* Form fields */}
</form>
);
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.