-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Refresh control with react-query is jumping #32836
Comments
I also came across with this issue but managed to solve with the following workaround:
Now use above hook for refresh purpose:
|
I came across this problem and I think the solution above me is might work because I did not test it myself, but i found this with not jumping or problem let fetchWeather = function () {
return fetch(
`http://api.weatherapi.com/v1/current.json?key=${myapikey}&q=${query}&aqi=no`
).then((res) => {
return res.json()
})
}
const { data, refetch } = useQuery('@weather', fetchWeather)
const [refresh, setRefresh] = useState(false) we can use the refetch function provided in the returned object in useQuery within a useCallBack hook and since refetch returns a promise we can do it like this and thanks to this you can track refresh state easily without jumping let onRefresh = useCallback(() => {
setRefresh(true)
refetch().then(() => setRefresh(false))
}, []) then finally <AreaView
refreshControl={
<RefreshControl refreshing={refresh} onRefresh={onRefresh} />
}>
{...{something}}
</AreaView> be aware the AreaView is a component made with styled component for ScrollView and it uses the same props that ScrollView provides |
I abstracted this into a hook: export function useUserRefresh<T>(refetch: () => Promise<T>) {
const [refreshing, setRefreshing] = useState(false);
const handleRefresh = useCallback(() => {
setRefreshing(true);
refetch().then(() => setRefreshing(false));
}, []);
return { refreshing, handleRefresh };
} You can then use it like this: const query = useQuery(/* */);
const { refreshing, handleRefresh } = useUserRefresh(query.refetch);
return (
<AreaView refreshControl={<RefreshControl refreshing={refreshing} onRefresh={handleRefresh} />}>
{/* do something with data */}
</AreaView>
); |
This still works beautifully, but with one fix. Instead of |
Custom Hook // useQueryRefresh.ts
export const useQueryRefresh = <T>(promiseFunctions: (() => Promise<T>)[]) => {
const [refreshing, setRefreshing] = useState(false);
const handleRefresh = useCallback(async () => {
setRefreshing(true);
await Promise.all(promiseFunctions.map((promiseFunction) => promiseFunction()));
setRefreshing(false);
}, [promiseFunctions]);
return { refreshing, handleRefresh };
}; Usage const { refreshing, handleRefresh } = useQueryRefresh([
() => queryClient.invalidateQueries({ queryKey: ['getUser'] }),
() => queryClient.invalidateQueries({ queryKey: ['getAccount'] }),
]);
<FlatList refreshing={refreshing} onRefresh={handleRefresh} /> |
also happening with apollo client |
I'm seeing this behavior with RTK Query as well. |
While the hooks fixes the flickering, the vibration feedback is gone on iPhone when using them. |
Actually looks like the haptic feedback gone is related to this other issue on tintColor: #43388 |
not happening on new arch (0.76) |
Description
Content inside
ScrollView
is jumping when I use react-query (useQuery hook) withRefreshControl
.RPReplay_Final1641463947.mov
Version
0.66.4
Output of
npx react-native info
Steps to reproduce
pull to refresh on real iOS device
Snack, code example, screenshot, or link to a repository
clone repo https://github.com/AleksandrNikolaevich/refresh-control-problem
or
App.js
package.json
P.S.
linked problem TanStack/query#2380
The text was updated successfully, but these errors were encountered: