-
How would one go about implementing the common action of pull to refresh with the At the moment it (refetch) is of type Providing an hook below as an example of how we've used refetch with other libs. interface Options {
refetch: () => Promise<void | any>;
}
function useRefreshControls({ refresh }: Options) {
const [refreshing, setRefreshing] = useState(false);
const requestRefresh = useCallback(
async function () {
try {
setRefreshing(true);
await refetch();
} finally {
setRefreshing(false);
}
},
[refresh]
);
return { refreshing, requestRefresh };
} And how it would be used with a ScrollView. const RefreshAwareScrollView: FC<Props> = ({ children, refresh, ...other }) => {
const { refreshing, requestRefresh } = useRefreshControls({ refresh });
return (
<ScrollView
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={requestRefresh} />}
{...other}>
{children}
</ScrollView>
);
}; Appreciate any thoughts on this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I assume you would just use the See https://redux-toolkit.js.org/rtk-query/usage/queries#query-loading-state |
Beta Was this translation helpful? Give feedback.
-
Solution for now was to create a separate hook to handle manual refresh passing in the import { useEffect, useMemo, useState } from 'react';
interface Options {
refresh: () => void;
isFetching: boolean;
}
const useManualRefresh = ({ refresh, isFetching }: Options) => {
const [refreshRequested, setRefreshRequested] = useState(false);
const requestRefresh = () => {
setRefreshRequested(true);
refresh();
};
useEffect(() => {
if (refreshRequested === true && isFetching === false) {
setRefreshRequested(false);
}
}, [refreshRequested, isFetching]);
const refreshing = useMemo(() => {
return refreshRequested === true && isFetching === true;
}, [isFetching, refreshRequested]);
return { requestRefresh, refreshing };
};
export { useManualRefresh }; |
Beta Was this translation helpful? Give feedback.
Solution for now was to create a separate hook to handle manual refresh passing in the
isFetching
flag like @phryneas mentioned.