Skip to content

Commit 38e7d6a

Browse files
committed
Update useNetworkState
1 parent b8de269 commit 38e7d6a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,18 +780,23 @@ export function useMouse() {
780780
return [state, ref];
781781
}
782782

783-
export function useNetworkState() {
784-
const connection =
783+
const getConnection = () => {
784+
return (
785785
navigator?.connection ||
786786
navigator?.mozConnection ||
787-
navigator?.webkitConnection;
787+
navigator?.webkitConnection
788+
);
789+
};
788790

791+
export function useNetworkState() {
789792
const cache = React.useRef({});
790793

791794
const subscribe = React.useCallback((callback) => {
792795
window.addEventListener("online", callback, { passive: true });
793796
window.addEventListener("offline", callback, { passive: true });
794797

798+
const connection = getConnection();
799+
795800
if (connection) {
796801
connection.addEventListener("change", callback, { passive: true });
797802
}
@@ -808,6 +813,7 @@ export function useNetworkState() {
808813

809814
const getSnapshot = () => {
810815
const online = navigator.onLine;
816+
const connection = getConnection();
811817

812818
const nextState = {
813819
online,

0 commit comments

Comments
 (0)