-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from martin-wahlberg/fix_hydration_errors_for…
…_useNetworkConnectivity_hook fix: stop useNetworkConnectivity from causing hydration errors.
- Loading branch information
Showing
11 changed files
with
72 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
/* eslint-disable n/no-callback-literal */ | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useSyncExternalStore } from 'react'; | ||
|
||
import { isWindowAvailable } from '../lib/user-agent.js'; | ||
const subscribeToNetworkConnectivity = (callback: () => void) => { | ||
window.addEventListener('online', callback); | ||
window.addEventListener('offline', callback); | ||
return () => { | ||
window.removeEventListener('online', callback); | ||
window.removeEventListener('offline', callback); | ||
}; | ||
}; | ||
|
||
const getNetworkConnectivitySnapshot = () => window.navigator.onLine; | ||
|
||
const getNetworkConnectivityServerSnapshot = () => false; | ||
|
||
export const useNetworkConnectivity = ( | ||
options: { | ||
onOnline?: (isOnline: boolean) => void; | ||
onOffline?: (isOnline: boolean) => void; | ||
} = {} | ||
) => { | ||
const [isOnline, setIsOnline] = useState(isWindowAvailable() ? navigator.onLine : false); | ||
|
||
const handleOnline = () => { | ||
setIsOnline(true); | ||
options.onOnline && options.onOnline(true); | ||
}; | ||
|
||
const handleOffline = () => { | ||
setIsOnline(false); | ||
options.onOffline && options.onOffline(false); | ||
}; | ||
const isOnline = useSyncExternalStore( | ||
subscribeToNetworkConnectivity, | ||
getNetworkConnectivitySnapshot, | ||
getNetworkConnectivityServerSnapshot | ||
); | ||
|
||
useEffect(() => { | ||
if (isWindowAvailable()) { | ||
window.addEventListener('online', handleOnline); | ||
window.addEventListener('offline', handleOffline); | ||
|
||
return () => { | ||
window.removeEventListener('online', handleOnline); | ||
window.removeEventListener('offline', handleOffline); | ||
}; | ||
if (isOnline) { | ||
options.onOnline && options.onOnline(true); | ||
} else if (options.onOnline) { | ||
options.onOffline && options.onOffline(false); | ||
} | ||
}, []); | ||
}, [isOnline, options]); | ||
|
||
return isOnline; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters