Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react fetch url

import { useEffect, useState } from 'react';

export const useFetch = (url) => {
  const [data, setData] = useState(null);
  const [isLoading, setLoading] = useState(false);
  const [error, setError] = useState(null);
  
  useEffect(() => {
    setLoading(true);
    fetch(url)
      .then(res => res.json)
      .then(setData)
      .catch(setError)
      .then(() => setLoading(false))
  }, [url]);
  
  return [data, isLoading, error];
};

function Profile() {
  const [data: profile, isLoading, error] = useFetch('/profile');
  return (
    <>
      {loading && <Spinner />}
      {data && <Profile data={data} />}
      {error && <Toast error={error} />}
    </>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js convert html to text 
Javascript :: js first letter to uppercase 
Javascript :: js load js file 
Javascript :: react native text input select all text on focus 
Javascript :: Cast to ObjectId failed for value 
Javascript :: convert to array str js 
Javascript :: File type node js 
Javascript :: regex email pattern 
Javascript :: javascript element read attribute 
Javascript :: jquery add multiple attribute to element by class 
Javascript :: make event nodejs 
Javascript :: getboundingclientrect() javascript 
Javascript :: js read from json1 
Javascript :: how to make a textarea unwritable in react native 
Javascript :: delete all node_modules folders recursively windows 
Javascript :: fetch javascript 
Javascript :: put 0 in front of month number javascript 
Javascript :: jquery window redirect with header 
Javascript :: js window.confirm 
Javascript :: how to set cookies in node js 
Javascript :: jquery get meta value 
Javascript :: await useeffect javascript 
Javascript :: json to list flutter 
Javascript :: Jquery handle change event of file upload created dynamically 
Javascript :: scroll down div from component angular 
Javascript :: bootstrap 5.1 3 tooltip not working 
Javascript :: how to use async await inside useeffect 
Javascript :: get the next character javascript 
Javascript :: NodeJS get rootpath of our project 
Javascript :: https://mongoosejs.com/docs/deprecations.html#findandmodify 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =