Junior — Middle+
Creating a custom React hook to fetch data from a specified URL
livecode
Task condition
It is necessary to write a custom hook useFetch that takes a URL string, initiates an HTTP request to that address, saves the received response, and returns it. Each time the hook is called, it should add the received results to a shared array and return it to the calling component.
import { useEffect, useState } from "react";
export default function App() {
const counter = 0;
const [data, setData] = useState("");
const [joke] = useFetch("https://api.chucknorris.io/jokes/random");
useEffect(() => {
console.log("was", joke);
setData(joke?.value);
}, [joke]);
return (
<div className="App">
<h1>Open the console {data}</h1>
</div>
);
}