Created
September 15, 2019 16:51
-
-
Save Landerson352/be4b73a31dbad5817d35ef19cbfeedbc to your computer and use it in GitHub Desktop.
A React Native hook for handling RefreshControl state & callbacks.
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
import React, { useRef, useState } from 'react'; | |
import { RefreshControl } from 'react-native'; | |
const useRefreshControl = (refreshesRequested = 1) => { | |
const refreshesCompleted = useRef(0); | |
const [isRefreshing, setIsRefreshing] = useState(false); | |
const handleRefreshStart = () => { | |
refreshesCompleted.current = 0; | |
setIsRefreshing(true); | |
}; | |
const handleRefreshEnd = () => { | |
refreshesCompleted.current += 1; | |
if (refreshesCompleted.current === refreshesRequested) { | |
setIsRefreshing(false); | |
} | |
}; | |
const refreshControl = ( | |
<RefreshControl | |
refreshing={isRefreshing} | |
onRefresh={handleRefreshStart} | |
/> | |
); | |
return [refreshControl, isRefreshing, handleRefreshEnd]; | |
}; | |
export default useRefreshControl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment