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 2 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
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
21 changes: 20 additions & 1 deletion template/src/navigation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react';
import React, { useEffect } from 'react';
import {
NavigationContainer,
DefaultTheme,
DarkTheme,
} from '@react-navigation/native';
import { AppState } from 'react-native';
import { focusManager, onlineManager } from '@tanstack/react-query';
import NetInfo from '@react-native-community/netinfo';
import RootNavigator from './RootNavigator';
import { withBoundary } from '~/components/boundary';
import { useColorSchemeValue } from '~/hooks/use-color-scheme-value';
Expand All @@ -14,6 +17,22 @@ function Navigation() {

useKillswitch();

useEffect(() => {
// Refetch queries on app focus
const subscription = AppState.addEventListener('change', (appState) => {
focusManager.setFocused(appState === 'active');
});

return () => subscription.remove();
}, []);

useEffect(() => {
// Refetch queries on reconnect
return NetInfo.addEventListener((state) => {
onlineManager.setOnline(!!state.isConnected);
});
}, []);
charlesdemers marked this conversation as resolved.
Show resolved Hide resolved

return (
<NavigationContainer
theme={theme}
Expand Down