Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Fetching data error and pending load check

import { useState, useEffect } from "react";

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

  useEffect(() => {
    fetch(url)
      .then((res) => {
        console.log(res);
        if (!res.ok) {
          throw Error("could not fetch data for that resource");
        }
        return res.json();
      })
      .then((data) => {
        setData(data);
        setIsPending(false);
        setError(null);
      })
      .catch((err) => {
        setIsPending(false);
        setError(err.message);
      });
  }, []);
  return { data, isPending, error };
};

export default useFetch;
Comment

PREVIOUS NEXT
Code Example
Javascript :: download string as file express js 
Javascript :: textarea events react testing library 
Javascript :: javascript if 
Javascript :: js switch 
Javascript :: react node-sass 
Javascript :: bouton scroll en haut 
Javascript :: jquery steps disable finish button 
Javascript :: collision javascript 
Javascript :: form submit jquery 
Javascript :: confetti canvas 
Javascript :: shallow render in react 
Javascript :: koa access request body 
Javascript :: await vuex dispatch true 
Javascript :: javascript Display Time Every 3 Second 
Javascript :: javascript find first element of array 
Javascript :: proxmox local storage path 
Javascript :: js similar jquery document ready 
Javascript :: react-native-image-picker npm 
Javascript :: update node two versions mac 
Javascript :: how to get value of tinymce in javascript 
Javascript :: how to add a key in a react element 
Javascript :: how to disable menu bar in browser using javascript 
Javascript :: function expression javascript 
Javascript :: how to host a react website 
Javascript :: javascript sort array 
Javascript :: setinterval 
Javascript :: gravity form on submit jquery 
Javascript :: angularjs - controllerAs 
Javascript :: joi.validate is not a function stack overflow 
Javascript :: GET and CHANGE the class of an element 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =