change directory to your src/
and execute the following comand:
git clone https://github.com/Newton-Nganga/awesome-react-fetch-hooks.git hooks/
Lastly execute this command to clean up the directory:
cd hooks/ ; sudo rm -r .git/
And viola! you can now import the hooks where you need them.
- Be using Reactjs or Vite
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable react-hooks/rules-of-hooks */
import { useEffect, useState } from "react";
export default function useDataFetch(url, options) {
const opt = options ? options : {};
const [responseData, setResponseData] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setLoading] = useState(false);
const fetchData = async () => {
setLoading(true);
try {
const response = await fetch(url, {
...opt,
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
...opt?.headers,
},
});
const data = await response.json();
setResponseData(data);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, [url, options]);
const refetch = () => {
fetchData();
};
return { responseData, error, isLoading, refetch };
}
After initializing the hook into your app
import useDataFetch from "hooks/useDataFetch";
Define the URL for the API endpoint and any necessary options for the fetch request.
const url = "http://localhost:8080/api/data";
const options = {
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer wsvhvccmnm"
}
};
- Handle Loading State First
- Handle Errors
- Display or Use Data
Check if the loading state is active and display a loading indicator.
if (isLoading) {
//do sth incase resource is still loading..
return <div>Loading...</div>;
}
Check if an error occurred and handle it accordingly.
//do sth incase an error is caught ...
if (error) {
return <div>Error: {error.message}</div>;
}
If loading is complete and no errors occurred, display the fetched data.
//do sth when the action is complete
return (
<>
{responseData && responseData.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</>
);
import React from "react";
import useUpdateData from "hooks/useUpdateData";
const DataUpdatingComponent = () => {
const userId="1003"
const url = `http://localhost:8080/api/data/${userId}`;
//options could be empty but url can't be null/empty
const options = {
headers: {
// "Content-Type": "application/json", --by default this is the set
"Authorization": "Bearer wsvhvccmnm"
}
//...more otions
};
const { responseData, error, isLoading, updateData } = useUpdateData(url, options);
//your handle update function
const handleUpdate = () => {
const updatedData = {
// the data/ fields you want to update
name:"John"
email: "[email protected]"
};
//call the upData method from the hook and pass in the updated data object
updateData(updatedData);
};
//handle loading state
if (isLoading) {
return <div>Loading...</div>;
}
//handle an error if its caught
if (error) {
return <div>Error: {error.message}</div>;
}
//do sth with the responseData returned
if(responseData.status.ok){
toast.success("item updated successfully")
}
return (
<>
//the rest of the app
<button onClick={handleUpdate}>Update Data</button>
// the rest of the app
</>
);
};
export default DataUpdatingComponent;
created by Newton for reusability.