Skip to content

Instantly share code, notes, and snippets.

@tjinlag
Created April 11, 2023 10:46
Show Gist options
  • Save tjinlag/c9531e370a9acb211ebff7b31e9e1812 to your computer and use it in GitHub Desktop.
Save tjinlag/c9531e370a9acb211ebff7b31e9e1812 to your computer and use it in GitHub Desktop.
React Custom Hook: useOnlineStatus
import { useState } from "react"
import useEventListener from "./useEventListener"
export default function useOnlineStatus() {
const [online, setOnline] = useState(navigator.onLine)
useEventListener("online", () => setOnline(navigator.onLine))
useEventListener("offline", () => setOnline(navigator.onLine))
return online
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment