-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #17400 from Expensify/marcaaron-useOnNetworkReconnect
`useOnNetworkReconnect()`
- Loading branch information
Showing
5 changed files
with
37 additions
and
41 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
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 |
---|---|---|
@@ -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; | ||
|
||
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]); | ||
} |
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