Skip to content
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 queries when the app gains focus and when changing the language #29

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions template/src/hooks/use-invalidate-queries-on-language-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useQueryClient } from '@tanstack/react-query';
import { useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';

export function useInvalidateQueriesOnLanguageChange() {
const { i18n } = useTranslation();
const languageRef = useRef(i18n.language);
const queryClient = useQueryClient();

useEffect(() => {
if (i18n.language === languageRef.current) {
return;
}

// Evict everything from the cache but don't refetch anything. Queries will
// be refetched when they become "enabled" when their screen gains focus.
queryClient.invalidateQueries({
refetchType: 'none',
});
}, [queryClient, i18n.language]);
}
13 changes: 13 additions & 0 deletions template/src/hooks/use-set-query-focus-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { focusManager } from '@tanstack/react-query';
import { useEffect } from 'react';
import { AppState } from 'react-native';

export function useSetQueryFocusManager() {
useEffect(() => {
const subscription = AppState.addEventListener('change', (appState) => {
focusManager.setFocused(appState === 'active');
});

return () => subscription.remove();
}, []);
}
11 changes: 11 additions & 0 deletions template/src/hooks/use-set-query-online-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect } from 'react';
import NetInfo from '@react-native-community/netinfo';
import { onlineManager } from '@tanstack/react-query';

export function useSetQueryOnlineManager() {
useEffect(() => {
return NetInfo.addEventListener((state) => {
onlineManager.setOnline(!!state.isConnected);
});
}, []);
}
8 changes: 8 additions & 0 deletions template/src/navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import RootNavigator from './RootNavigator';
import { withBoundary } from '~/components/boundary';
import { useColorSchemeValue } from '~/hooks/use-color-scheme-value';
import { useKillswitch } from '~/hooks/use-killswitch';
import { useSetQueryFocusManager } from '~/hooks/use-set-query-focus-manager';
import { useSetQueryOnlineManager } from '~/hooks/use-set-query-online-manager';
import { useInvalidateQueriesOnLanguageChange } from '~/hooks/use-invalidate-queries-on-language-change';

function Navigation() {
const theme = useColorSchemeValue([DefaultTheme, DarkTheme]);

useKillswitch();

useSetQueryFocusManager();
useSetQueryOnlineManager();

useInvalidateQueriesOnLanguageChange();

return (
<NavigationContainer
theme={theme}
Expand Down