Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fetchapi snippet

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 :: summernote click event jquery 
Javascript :: GET / - - ms - - node js 
Javascript :: jtml cdn enter 
Javascript :: A class I made for creating OOP styled JSON objects 
Javascript :: get members of a group graph pnp js 
Javascript :: javascript shorthand ternary 
Javascript :: form validation jflutter 
Javascript :: cypher neo4j 
Javascript :: how stop users from submitting empty input in todo list javascript 
Javascript :: how to create custom callback function in javascript 
Javascript :: how to append response header in node in every request 
Javascript :: convert milliseconds to hours minutes seconds days javascript 
Javascript :: straforma in string js 
Javascript :: rebuild package-lock.json 
Javascript :: birth day quote 
Javascript :: value.js package 
Javascript :: patch if else use 
Javascript :: logo ticker javascript 
Javascript :: reduce example 
Javascript :: babel 7 ReferenceError: regeneratorRuntime 
Javascript :: save new 
Javascript :: crypto digest node.js 
Javascript :: javascript find the smallest and biggest number in array 
Javascript :: how to parse arguments to a promise in javascript 
Javascript :: array.includes is not a function react 
Javascript :: how to change div style to full page react 
Javascript :: how to poll every n seconds in react js 
Javascript :: create extern to be usable in c# 
Javascript :: declerative and imperative program combine 
Javascript :: convert low high to integer in js 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =