-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f42453
commit 7ff81ec
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { toast } from "react-toastify"; | ||
|
||
const ErrorToast = ({ message }) => { | ||
toast.error(message); | ||
return null; // Returning null avoids unnecessary rendering | ||
}; | ||
|
||
const checkInternetConnection = () => { | ||
const [isConnected, setIsConnected] = useState(navigator.onLine); | ||
const [prevConnected, setPrevConnected] = useState(navigator.onLine); | ||
|
||
useEffect(() => { | ||
const handleOnlineEvent = () => setIsConnected(true); | ||
const handleOfflineEvent = () => setIsConnected(false); | ||
|
||
window.addEventListener("online", handleOnlineEvent); | ||
window.addEventListener("offline", handleOfflineEvent); | ||
|
||
return () => { | ||
window.removeEventListener("online", handleOnlineEvent); | ||
window.removeEventListener("offline", handleOfflineEvent); | ||
}; | ||
}, []); | ||
|
||
if (!isConnected) { | ||
return <ErrorToast message="No internet connection" />; | ||
} | ||
|
||
|
||
return null; | ||
}; | ||
|
||
export default checkInternetConnection; |