Skip to content

This is the custom react hook which you can use to call the api and it will return the data,error(if any),loading status

Notifications You must be signed in to change notification settings

iamrajput/react-custom-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-custom-fetch-hook

GitHub issues npm version GitHub stars

React hook for conveniently use Fetch API. It will take url and the extra header if needed.

import useFetch from 'react-custom-fetch-hook'

function CustomHook() {
    const {data,loading} = useFetch('https://quotable.io/quotes',{})
  
    if(loading){
        return <h3>Loading...</h3>
    }
    return (
        <div>
            {data.results.map((result) => (
                   <p key={result._id}>{result.content}</p>
            ))}
        </div>
    )
}

useFetch accepts the same arguments as fetch function.

Installation

Install it with npm:

npm i react-custom-fetch-hook --save

Error handling

The useFetch hook returns an error field at any fetch exception. The error field extends Error and has status and statusText fields equal to Response.

function CustomHook() {
  const {data,loading ,error } = useFetch("https://quotable.io/quotes");

  if (error) {
    return <div>
      <p>Code: ${error.status}</p>
      <p>Message: ${error.statusText}</p>
    </div>
  }
};

About

This is the custom react hook which you can use to call the api and it will return the data,error(if any),loading status

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published