Skip to content

geekskai/react-network-detect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Gan Kai
Aug 13, 2022
8171f96 Β· Aug 13, 2022

History

43 Commits
Aug 13, 2022
Aug 13, 2022
Jun 10, 2022
Jun 11, 2022
Jun 11, 2022
Jun 11, 2022
Jun 10, 2022
Aug 13, 2022
Aug 13, 2022
Aug 13, 2022
Jun 12, 2022
Aug 13, 2022

Repository files navigation

react-network-detect

npm bundle size GitHub license npm npm downloads GitHub stars

react network detection hooks

If you think it's good, please give a star, this will be the motivation for me to continue to work hard, thank you very much.

install

npm install react-network-detect

or

yarn add react-network-detect

demo

codesandbox

use

useOnlineEffect

import { useOnlineEffect } from 'react-network-detect';


function App() {
  const { isOnline } = useOnlineEffect();

  return (
    <div className="App">
     {
        isOnline?'this is online':'oh network error!'
     }
    </div>
  );
}

useNetworkStatus

import { useNetworkStatus } from 'react-network-detect';

const App = () => {
  const { effectiveConnectionType } = useNetworkStatus();

  let media;
  switch(effectiveConnectionType) {
    case 'slow-2g':
      media = <img src='...' alt='low resolution' />;
      break;
    case '2g':
      media = <img src='...' alt='medium resolution' />;
      break;
    case '3g':
      media = <img src='...' alt='high resolution' />;
      break;
    case '4g':
      media = <video muted controls>...</video>;
      break;
    default:
      media = <video muted controls>...</video>;
      break;
  }
  
  return <div>{media}</div>;
};