Skip to content

Commit

Permalink
Finish feature/27
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcastillop authored Jan 8, 2025
2 parents c68afef + a47e39d commit 766a46a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
26 changes: 13 additions & 13 deletions snippets/js.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -1009,26 +1009,26 @@
"useOffline": {
"prefix": "useOffline",
"body": [
"import { useEffect, useState } from 'react';",
"import { useSyncExternalStore } from 'react';",
"",
"export const useOffline = () => {",
" const [offline, setOffline] = useState(null);",
" const getSnapshot = () => !navigator.onLine;",
"",
" useEffect(() => {",
" const handleNetworkState = () => {",
" setOffline(!offline);",
" };",
" addEventListener('offline', handleNetworkState);",
" addEventListener('online', handleNetworkState);",
" const subscribe = (callback) => {",
" const handleNetworkChange = () => callback();",
"",
" window.addEventListener('online', handleNetworkChange);",
" window.addEventListener('offline', handleNetworkChange);",
"",
" return () => {",
" removeEventListener('online', handleNetworkState);",
" removeEventListener('offline', handleNetworkState);",
" window.removeEventListener('online', handleNetworkChange);",
" window.removeEventListener('offline', handleNetworkChange);",
" };",
" }, [offline]);",
" };",
"",
" return !!offline;",
"};"
" return useSyncExternalStore(subscribe, getSnapshot);",
"};",
""
],
"description": "Reaction hook to track if user is offline or not"
},
Expand Down
28 changes: 13 additions & 15 deletions snippets/ts.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -1098,28 +1098,26 @@
"useOffline": {
"prefix": "useOffline",
"body": [
"import { useEffect, useState } from 'react';",
"import { useSyncExternalStore } from 'react';",
"",
"type TUseOffline = () => boolean;",
"export const useOffline = () => {",
" const getSnapshot = () => !navigator.onLine;",
"",
"export const useOffline: TUseOffline = () => {",
" const [offline, setOffline] = useState<boolean | null>(null);",
" const subscribe = (callback: () => void) => {",
" const handleNetworkChange = () => callback();",
"",
" useEffect(() => {",
" const handleNetworkState = () => {",
" setOffline(!offline);",
" };",
" addEventListener('offline', handleNetworkState);",
" addEventListener('online', handleNetworkState);",
" window.addEventListener('online', handleNetworkChange);",
" window.addEventListener('offline', handleNetworkChange);",
"",
" return () => {",
" removeEventListener('online', handleNetworkState);",
" removeEventListener('offline', handleNetworkState);",
" window.removeEventListener('online', handleNetworkChange);",
" window.removeEventListener('offline', handleNetworkChange);",
" };",
" }, [offline]);",
" };",
"",
" return !!offline;",
"};"
" return useSyncExternalStore(subscribe, getSnapshot);",
"};",
""
],
"description": "Reaction hook to track if user is offline or not"
},
Expand Down

0 comments on commit 766a46a

Please sign in to comment.