17
loading...
This website collects cookies to deliver better user experience
useState
and useEffect
, with values like loading
, data
and error
e.g This example, this hook is to help abstract that functionality into one simple hook that can be used anywhere and multiple times.create-react-app
boiler template for typescript and the only external library we would be using is axios
for data fetching.yarn create react-app use-fetch --template typescript
# for npm
npx create-react-app use-fetch --template typescript
axios
cd use-fetch
yarn add axios
# for npm
npm install axios
src
directory delete the following file (because they aren't needed)src
directory create another directory called hooks
, this is where our hook will reside.cd src
mkdir hooks
hooks
directory create a file called useFetch.tsx
.useFetch
file.import { useState, useEffect, useCallback } from "react";
import axios from "axios";
interface UseFetchProps {
url: string;
}
const useFetch = ({ url }: UseFetchProps) => {
const [data, setData] = useState<any>();
const [error, setError] = useState(false);
// function to fetch data
const fetch = useCallback(async () => {
setError(false);
try {
const fetchedData = await axios.get(url);
setData(fetchedData.data);
} catch {
setError(true);
}
}, [url]);
useEffect(() => {
// on first load fetch data
fetch();
}, [fetch]);
return {
data,
error,
revalidate: fetch,
};
};
export default useFetch;
url
, which is the API url at which we want to fetch data from. It has two states data
and error
which are used to store data gotten from the API and check for errors respectively.fetch
and wrapped it within a useCallback
hook, Visit here to see the reason why we used a useCallback
hook.useEffect
hook to run the fetch
function as soon as the hook is mounted 🙂.data
, error
and revalidate
which is the fetch
function for when we want to programmatically revalidate the data.App.tsx
import useFetch from "./hooks/useFetch";
import logo from "./logo.svg";
function App() {
const { error, data, revalidate } = useFetch({
url: "https://random-data-api.com/api/users/random_user?size=5",
});
if (!data) {
return <h2>Loading...</h2>;
}
if (error) {
return <h2>Error fetching users</h2>;
}
return (
<div className="App">
<img src={logo} alt="react logo" />
<h1 className="title">useFetch()</h1>
<button onClick={revalidate}>revalidate</button>
<div className="items">
{data.map((el: any) => (
<div className="item" key={el.uid}>
<img
src={`https://avatars.dicebear.com/api/big-smile/${el.first_name}.svg`}
alt={`${el.username} profile`}
className="item__img"
/>
<div className="item__info">
<p className="name">
{el.first_name} {el.last_name}{" "}
<span className="username">(@{el.username})</span>
</p>
<p className="job">{el.employment.title}</p>
<p
className={`status ${
el.subscription.status.toLowerCase() === "active"
? "success"
: el.subscription.status.toLowerCase() === "blocked"
? "danger"
: "warn"
}`}
>
{el.subscription.status}
</p>
</div>
</div>
))}
</div>
</div>
);
}
export default App;
useFetch
hook. Lets and more props.interface UseFetchProps {
url: string;
revalidate?: boolean;
interval?: number;
}
revalidate
will be a boolean to check if we want to implement interval revalidation or not, interval
will be the time taken between every revalidation (in seconds)....
const useFetch = ({ url, revalidate, interval }: UseFetchProps) => {
...
revalidateKey
that we will change on every interval which will be added to our useEffect
dependency array. Adding this to our dependency array will ensure that the function within our useEffect
will run everytime the revalidateKey
changes.revalidateKey
, we will create a new useEffect
that has a setInterval
....
const [revalidateKey, setRevalidateKey] = useState("");
...
useEffect(() => {
const revalidateInterval = setInterval(() => {
if (revalidate) {
setRevalidateKey(Math.random().toString());
}
// if no interval is given, use 3 seconds
}, (interval ? interval : 3) * 1000);
return () => clearInterval(revalidateInterval);
}, [interval, revalidate]);
useFetch
hook should then look something like this.const useFetch = ({ url, revalidate, interval }: UseFetchProps) => {
const [revalidateKey, setRevalidateKey] = useState("");
const [data, setData] = useState<any>();
const [error, setError] = useState(false);
// function to fetch data
const fetch = useCallback(async () => {
setError(false);
try {
const fetchedData = await axios.get(url);
setData(fetchedData.data);
} catch {
setError(true);
}
}, [url]);
useEffect(() => {
const revalidateInterval = setInterval(() => {
if (revalidate) {
setRevalidateKey(Math.random().toString());
}
// if no interval is given, use 3 seconds
}, (interval ? interval : 3) * 1000);
return () => clearInterval(revalidateInterval);
}, [interval, revalidate]);
useEffect(() => {
// on first load fetch data and when revalidateKey changes
fetch();
}, [fetch, revalidateKey]);
return {
data,
error,
revalidate: fetch,
};
};
useFetch
hook ✨const { error, data, revalidate } = useFetch({
url: "https://random-data-api.com/api/users/random_user?size=5",
revalidate: false,
// fetch every 5 seconds
interval: 5,
});
GET
method, and Graphql uses POST
method for data fetching. To make the hook more dynamic you can add more props like isGraphql
and query
, isGraphql
will be a boolean to check if its Graphql or REST so you can have a condition in your hook to use axios.post()
instead of axios.get()
and query
for the graphql query.