Skip to content

Instantly share code, notes, and snippets.

@Landerson352
Created September 15, 2019 16:51
Show Gist options
  • Save Landerson352/be4b73a31dbad5817d35ef19cbfeedbc to your computer and use it in GitHub Desktop.
Save Landerson352/be4b73a31dbad5817d35ef19cbfeedbc to your computer and use it in GitHub Desktop.
A React Native hook for handling RefreshControl state & callbacks.
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