Return to site

🛜🪝 USEONLINESTATUS HOOK (REACT): DETECT ONLINE/OFFLINE (BROWSER EVENTS)

January 24, 2026

🔸 TL;DR

▪️ A small React hook to track online/offline status in real time 🛜

▪️ Great for resilient UX: banners, disabling actions, retry/sync 🔁

▪️ Remember: “online” ≠ “internet is reachable” ⚠️

🔸 WHY THIS HOOK EXISTS

Ever shipped a form that fails silently when the user loses connectivity? 😅

This tiny hook lets your UI react instantly to browser online/offline changes: show a banner 🚧, disable submit 🧾, pause sync 🔄, etc.

🔸 THE HOOK (CODE SNIPPET)

🔸 HOW TO USE IT

▪️ Show an offline banner 🚫📶

▪️ Disable “Submit” when offline 🛑

▪️ Queue actions and replay when back online 🧰

▪️ Trigger a “resync” when the user reconnects 🔄

Example:

🔸 IMPORTANT NOTE (DON’T OVERPROMISE) ⚠️

▪️ navigator.onLine + online/offline events reflect network connectivity, not guaranteed “internet access” (captive portals, DNS issues, flaky Wi-Fi…).

▪️ For critical flows, combine this with a lightweight “ping” (e.g., fetch("/health")) ✅

🔸 TAKEAWAYS

▪️ Listen to online/offline once, cleanup listeners properly ✅

▪️ Build network-aware UI patterns (queue, retry, sync) 🧠

▪️ Add an optional server check for high-stakes actions 🔍

#react #javascript #webdev #frontend #hooks #pwa #ux #softwareengineering #resilience #performance

🏆 Thrive at React job interviews: https://www.udemy.com/course/react-reactjs-questions-and-answers-2023-job-interviews/?referralCode=C52F8C70EFA5555C18AC