-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
25 lines (19 loc) · 645 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"use strict";
let { useState, useEffect } = require("react");
function getConnection() {
return navigator.connection || navigator.mozConnection || navigator.webkitConnection;
}
function useNetworkStatus() {
let [connection, updateNetworkConnection] = useState(getConnection());
useEffect(() => {
function updateConnectionStatus() {
updateNetworkConnection(getConnection());
}
connection.addEventListener("change", updateConnectionStatus);
return () => {
connection.removeEventListener("change", updateConnectionStatus);
};
}, [connection]);
return connection;
}
module.exports = useNetworkStatus;