1.0.0 • Published 4 years ago

@calebdeji/use-network-status v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

use-network-status

intercept network and get the status

NPM JavaScript Style Guide

Install

npm install --save use-network-status
  • It's pretty small in size
  • It's build with typescript

Usage

import React, { useState, useEffect } from 'react'

import useNetworkStatus from 'use-network-status'

const moviesEndpoint = 'https://ghibliapi.herokuapp.com/films'

const Example = () => {
  const { networkStatusCode, clearStatus } = useNetworkStatus({
    urls: [moviesEndpoint]
  });

  const [data, setData] = useState(null)

  useEffect(() => {
    fetch(moviesEndpoint)
      .then((response) => {
          return response.json();
      })
      .then((data) => {
          setData(data)
      });
  , [])

  if( networkStatusCode[moviesEndpoint] === 401 ){
    return <p> You are not authorised </p>
  }

  return <>
    {
      data.map(({ id, description })=>{
        return (
          <div key = {id}> <p> { description } </p> </div>
        )
      })
    }
  </>
}

License

MIT © calebdeji


This hook is created using create-react-hook.

1.0.0

4 years ago