Skip to content

Commit 4ce13b7

Browse files
committed
Use useRef hook to track previous offline state
1 parent dcbf58e commit 4ce13b7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/react-radfish/Application/index.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { Toast } from "../alerts";
2-
import { createContext, useEffect, useContext } from "react";
2+
import { createContext, useEffect, useContext, useRef } from "react";
33
import { useOfflineStatus, useToasts, dispatchToast } from "../hooks";
44

55
const ApplicationContext = createContext();
66

77
function ApplicationComponent(props) {
88
const { toasts } = useToasts();
99
const { isOffline } = useOfflineStatus();
10+
const prevIsOffline = useRef(null);
1011

1112
useEffect(() => {
12-
if (!isOffline) {
13-
dispatchToast({ message: "Application is online", status: "info", duration: 3000 });
14-
}
15-
}, [isOffline]);
13+
if (prevIsOffline.current === null) {
14+
prevIsOffline.current = isOffline;
15+
} else if (prevIsOffline.current && !isOffline) {
16+
dispatchToast({ message: "Application is online", status: "info", duration: 3000 });
17+
}
18+
prevIsOffline.current = isOffline;
19+
}, [isOffline]);
1620

1721
return (
1822
<div className="radfish__application">

0 commit comments

Comments
 (0)