Answers for "React hook useFetch"

0

React hook useFetch

import { useEffect, useState } from 'react';
    
export default function useFetch(url) {
    const [data, setData] = useState(null);
    useEffect(() => {
        async function loadData() {
            const response = await fetch(url);
            if(!response.ok) {
                // oups! something went wrong
                return;
            }
    
            const posts = await response.json();
            setData(posts);
        }
    
        loadData();
    }, [url]);
    return data;
}
Posted by: Guest on August-12-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language