mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
fix useOffline having incorrect state when listener isn’t fired
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
export default function useOffline() {
|
||||
const [isOffline, setIsOffline] = useState(true);
|
||||
const [isOffline, setIsOffline] = useState(false);
|
||||
const handleOffline = () => setIsOffline(true);
|
||||
const handleOnline = () => setIsOffline(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Need to set here because the listener doesn’t fire on initial load
|
||||
setIsOffline(!window.navigator.onLine);
|
||||
|
||||
window.addEventListener("offline", handleOffline);
|
||||
window.addEventListener("online", handleOnline);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user