React hook useNetworkStatus()
returns whether the client's browser online with updates when the network state is changed.
The easiest way to use useNetworkStatus
React hook is to install it from npm or yarn.
npm install use-network-status --save
Or
yarn add use-network-status
Pull the hook into your component (usually the root one) and call the hook inside the functional component.
import { useNetworkStatus } from "use-network-status";
function App() {
const isOnline = useNetworkStatus();
return (
<div className="status">
{isOnline ? '📡 Online' : '📴 Offline'}
</div>
);
}