skip to Main Content
import { useEffect, useState } from "react";
const useFetch = () => {

    const [data, setData] = useState(null);
    const [isPending, setIsPending] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {

        const abortCont = new AbortController()
        setTimeout(() => {
            fetch('http://localhost:3000/blogs', {signal: AbortController.signal})
            .then(res => {
                console.log(res)

                if(!res.ok){
                    throw Error('Could no fetch the data for that resource')
                }
                return res.json();
                
            })
            .then(data => {
                setData(data)
                console.log(data)
                setIsPending(false)
                setError(null)
                })
                .catch(err => {
                    if(err.name === 'AbortError')
                    console.log('fetch aborted')
                    setIsPending(false)
                    setError(err.message)
                
            }, 1000)
            });
            return() => abortCont.abort()
        }, []);

        return {data, isPending, error}
}

export default useFetch;

i was trying to view my blogs by id and i am receiving a blank page

2

Answers


  1. Try with this

    import { useEffect, useState } from "react";
    
    const useFetch = () => {
      const [data, setData] = useState(null);
      const [isPending, setIsPending] = useState(true);
      const [error, setError] = useState(null);
    
      useEffect(() => {
        const abortCont = new AbortController();
        setTimeout(() => {
          fetch("http://localhost:3000/blogs", { signal: abortCont.signal })
            .then((res) => {
              console.log(res);
              if (!res.ok) {
                throw Error("Could not fetch the data for that resource");
              }
              return res.json();
            })
            .then((data) => {
              setData(data);
              setIsPending(false);
              setError(null);
            })
            .catch((err) => {
              if (err.name === "AbortError") {
                console.log("Fetch aborted");
              } else {
                setIsPending(false);
                setError(err.message);
              }
            });
        }, 1000);
    
        return () => abortCont.abort();
      }, []);
    
      return { data, isPending, error };
    };
    
    export default useFetch;
    
    Login or Signup to reply.
  2. There’s a small error in your code. You’re utilizing a time value in
    the catch block, but you can use it in setTimeout instead.

    import { useEffect, useState } from "react";
        const useFetch = () => {
        
            const [data, setData] = useState(null);
            const [isPending, setIsPending] = useState(true);
            const [error, setError] = useState(null);
        
            useEffect(() => {
        
                const abortCont = new AbortController()
                setTimeout(() => {
                    fetch('http://localhost:3000/blogs', {signal: AbortController.signal})
                    .then(res => {
                        console.log(res)
        
                        if(!res.ok){
                            throw Error('Could no fetch the data for that resource')
                        }
                        return res.json();
                        
                    })
                    .then(data => {
                        setData(data)
                        console.log(data)
                        setIsPending(false)
                        setError(null)
                        })
                        .catch(err => {
                            if(err.name === 'AbortError')
                            console.log('fetch aborted')
                            setIsPending(false)
                            setError(err.message)
                        
                    })
                    },1000);
                    return() => abortCont.abort()
                }, []);
        
                return {data, isPending, error}
        }
        
        export default useFetch;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search