-
Notifications
You must be signed in to change notification settings - Fork 3.9k
useOnNetworkReconnect()
#17400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useOnNetworkReconnect()
#17400
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {useRef, useContext, useEffect} from 'react'; | ||
| import {NetworkContext} from '../OnyxProvider'; | ||
|
|
||
| /** | ||
| * @param {Function} onNetworkReconnect | ||
| */ | ||
| export default function useOnNetworkReconnect(onNetworkReconnect) { | ||
| const callback = useRef(onNetworkReconnect); | ||
| callback.current = onNetworkReconnect; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think this is necessary? I think providing an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes because the function itself could change e.g. if it was passed as a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: Ah, I see now. However, it could be optimized further with the use of useEffect(() => {
callback.current = onNetworkReconnect;
}, [onNetworkReconnect]);
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introducing The expression: callback.current = onNetworkReconnect;sets an object key to point to a memory address, there's no heavy computation to mitigate by introducing an effect hook (it's not like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explaining that, @kidroca. I see your point now and agree that using the direct assignment is the better approach as it avoids any potential timing issues. Thanks again for your input! |
||
|
|
||
| const {isOffline} = useContext(NetworkContext); | ||
| const prevOfflineStatusRef = useRef(isOffline); | ||
| useEffect(() => { | ||
| // If we were offline before and now we are not offline then we just reconnected | ||
| const didReconnect = prevOfflineStatusRef.current && !isOffline; | ||
| if (!didReconnect) { | ||
| return; | ||
| } | ||
|
|
||
| callback.current(); | ||
| }, [isOffline]); | ||
|
|
||
| useEffect(() => { | ||
| // Used to store previous prop values to compare on next render | ||
| prevOfflineStatusRef.current = isOffline; | ||
| }, [isOffline]); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.