T
TanStack3y ago
passive-yellow

storagePersister and staleTime Infinity

Hello, I understand that createAsyncStoragePersister and createSyncStoragePersister create a snapshot of the current queryClient. If I use "staleTime: Infinity" the query will never be called again if I don't invalidate it. That said, is there any property I can set so that the data is always invalidated when the app (React Native) is restarted? Or is doing it manually already good?
1 Reply
rival-black
rival-black3y ago
Do you want it to happen everytime the app refocuses or only starting? Not enterily sure if this fits your usecase but maybe use focusManger?
import { useEffect } from 'react';
import { AppState } from 'react-native';

export function useAppState(onChange) {
useEffect(() => {
AppState.addEventListener('change', onChange);
return () => {
AppState.removeEventListener('change', onChange);
};
}, [onChange]);
}
-----

function onAppStateChange(status) {
if (Platform.OS !== 'web') {
focusManager.setFocused(status === 'active');

if(status ==='active') {
// run your code here or have another listener or whatever fits you
}
}
}

export default function App() {
useAppState(onAppStateChange);

}
import { useEffect } from 'react';
import { AppState } from 'react-native';

export function useAppState(onChange) {
useEffect(() => {
AppState.addEventListener('change', onChange);
return () => {
AppState.removeEventListener('change', onChange);
};
}, [onChange]);
}
-----

function onAppStateChange(status) {
if (Platform.OS !== 'web') {
focusManager.setFocused(status === 'active');

if(status ==='active') {
// run your code here or have another listener or whatever fits you
}
}
}

export default function App() {
useAppState(onAppStateChange);

}
Or maybe you can use the onSuccess on Provider? (Haven't tried it, but should work something like this)
onSuccess={() =>
queryClient.invalidateQueries()
}
onSuccess={() =>
queryClient.invalidateQueries()
}

Did you find this page helpful?