Skip to content

Commit f8fdebf

Browse files
committed
feat: add useNetwork hooks
1 parent 4ba264e commit f8fdebf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/hooks/web/useNetwork.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ref, onBeforeUnmount } from 'vue'
2+
3+
const useNetwork = () => {
4+
const online = ref(true)
5+
6+
const updateNetwork = () => {
7+
online.value = navigator.onLine
8+
}
9+
10+
window.addEventListener('online', updateNetwork)
11+
window.addEventListener('offline', updateNetwork)
12+
13+
onBeforeUnmount(() => {
14+
window.removeEventListener('online', updateNetwork)
15+
window.removeEventListener('offline', updateNetwork)
16+
})
17+
18+
return { online }
19+
}
20+
21+
export { useNetwork }

0 commit comments

Comments
 (0)