Skip to content

Commit

Permalink
Refresh queries when the app gains focus and when changing the language
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdemers committed Oct 10, 2023
1 parent 845638f commit 934a472
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 19 additions & 1 deletion template/src/navigation/RootNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
NativeStackNavigationProp,
NativeStackScreenProps,
} from '@react-navigation/native-stack';
import React from 'react';
import React, { useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { useQueryClient } from '@tanstack/react-query';
import { HomeScreen } from '../screens/Home';
import { ExampleBottomSheet } from '~/screens/bottom-sheets/ExampleBottomSheet';
import { SecretConfigScreen } from '~/screens/SecretConfig';
Expand All @@ -24,10 +26,26 @@ export type RootStackScreenProps<Screen extends keyof RootStackParamList> =
const Stack = createNativeStackNavigator<RootStackParamList>();

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

const secretPanelEnabled = useApplicationConfiguration(
'SECRET_PANEL_ENABLED'
);

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]);

return (
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Home" component={HomeScreen} />
Expand Down
7 changes: 7 additions & 0 deletions template/src/navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import {
DefaultTheme,
DarkTheme,
} from '@react-navigation/native';
import { AppState } from 'react-native';
import { focusManager } from '@tanstack/react-query';
import RootNavigator from './RootNavigator';
import { withBoundary } from '~/components/boundary';
import { useColorSchemeValue } from '~/hooks/use-color-scheme-value';
import { useKillswitch } from '~/hooks/use-killswitch';

// Notify react-query when the focused state changes
AppState.addEventListener('change', (appState) => {
focusManager.setFocused(appState === 'active');
});

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

Expand Down

0 comments on commit 934a472

Please sign in to comment.