How to Detect 'Offline' State in React

Detecting an offline state in React applications is crucial for enhancing user experience, ensuring the app behaves correctly even without an internet connection. Hereβs how to implement this with React Hooks.
Direct Solution with Code
To detect the offline state, we will use the useState and useEffect hooks from React. The solution involves listening to the browser's online and offline events.
import React, { useState, useEffect } from 'react';
const useOfflineDetector = () => {
const [isOffline, setIsOffline] = useState(!navigator.onLine);
useEffect(() => {
const goOnline = () => setIsOffline(false);
const goOffline = () => setIsOffline(true);
window.addEventListener('online', goOnline);
window.addEventListener('offline', goOffline);
return () => {
window.removeEventListener('online', goOnline);
window.removeEventListener('offline', goOffline);
};
}, []);
return isOffline;
};
const App = () => {
const isOffline = useOfflineDetector();
return (
<div>
{isOffline ? (
<p>You are currently offline. Some features may not be available.</p>
) : (
<p>You are online.</p>
)}
</div>
);
};
export default App;
Explanation of Key Concepts
- useState Hook: Initializes the
isOfflinestate to the current browser online status usingnavigator.onLine. This state will reflect whether the app is offline or online. - useEffect Hook: Sets up event listeners for
onlineandofflineevents when the component mounts. These listeners update theisOfflinestate accordingly. It's important to clean up the listeners when the component unmounts to avoid memory leaks. - navigator.onLine: A read-only Boolean value that returns
trueif the browser has network access; otherwise,false. It serves as our initial state value.
Quick Tip
While navigator.onLine provides a quick way to check the online status, it's not always reliable, especially in complex network conditions. For critical features, consider implementing additional checks, such as attempting to fetch a small resource from your server to verify connectivity.
Gotcha
Remember, the online and offline events only tell you about the browser's network status, not the actual internet access. A device connected to a router without internet access will still trigger an online event.
Incorporating offline detection enhances user experience by providing feedback on their connection status and potentially enabling offline functionalities. This simple yet effective approach ensures your React app remains responsive and informative, regardless of network conditions.