Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdemers committed Oct 10, 2023
1 parent 23d1451 commit fdef763
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
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);
});
}, []);
}
25 changes: 5 additions & 20 deletions template/src/navigation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import React, { useEffect } from 'react';
import React 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';
import { useKillswitch } from '~/hooks/use-killswitch';
import { useSetQueryFocusManager } from '~/hooks/use-set-query-focus-manager';
import { useSetQueryOnlineManager } from '~/hooks/use-set-query-online-manager';

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

return (
<NavigationContainer
Expand Down

0 comments on commit fdef763

Please sign in to comment.