Skip to content

Commit

Permalink
Wrap subscription in effect and add onlineManager
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdemers committed Oct 10, 2023
1 parent 934a472 commit 23d1451
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions template/src/navigation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import React from 'react';
import React, { useEffect } from 'react';
import {
NavigationContainer,
DefaultTheme,
DarkTheme,
} from '@react-navigation/native';
import { AppState } from 'react-native';
import { focusManager } from '@tanstack/react-query';
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';
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]);

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);
});
}, []);

return (
<NavigationContainer
theme={theme}
Expand Down

0 comments on commit 23d1451

Please sign in to comment.