react hook usefetch

Solutions on MaxInterview for react hook usefetch by the best coders in the world

showing results for - "react hook usefetch"
Imani
01 Apr 2020
1import { useEffect, useState } from 'react';
2    
3export default function useFetch(url) {
4    const [data, setData] = useState(null);
5    useEffect(() => {
6        async function loadData() {
7            const response = await fetch(url);
8            if(!response.ok) {
9                // oups! something went wrong
10                return;
11            }
12    
13            const posts = await response.json();
14            setData(posts);
15        }
16    
17        loadData();
18    }, [url]);
19    return data;
20}
21