Refetch usage with React Native and fixing "Cannot refetch a query that has not been started yet" #3512
Replies: 5 comments 7 replies
-
Downgrading to 1.8.5 resolves the issue, but being stuck with an old version is not really a long term solution. |
Beta Was this translation helpful? Give feedback.
-
I'm running into the exact same issue, minus the React Native setup. It's happening for me in a component unit test. The component calls Started happening when I upgraded from 1.7.5 to 1.9.5, upgraded because I needed the endpoint-level |
Beta Was this translation helpful? Give feedback.
-
@git-commit did you found solution for the problem with |
Beta Was this translation helpful? Give feedback.
-
I'm also having the same issue with 1.9.5: I have a submit form func like so: const { data: userInfo, refetch } = useGetUserInfoQuery();
const [transmitTransmission, { isLoading }] = useTransmitTransmissionMutation();
const handleTransmitTransmissionSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
try {
await transmitTransmission({ id }).unwrap();
refetch();
dispatch(setNotification(newSuccessNotification('Successful!', '')));
} catch (error) {
console.log(error);
dispatch(setNotification(newErrorNotification('Error', handleError(error))));
}
}; On submit, refetch will throw an error Any luck @sobczakDev @git-commit @edmondkong-storyful ? |
Beta Was this translation helpful? Give feedback.
-
import { ThunkDispatch, createAction } from "@reduxjs/toolkit";
import { addNetworkStateListener } from "expo-network";
import {
Platform,
AppState,
AppStateStatus,
NativeEventSubscription,
} from "react-native";
import { type EventSubscription } from "expo-modules-core";
export const onFocus = createAction("__rtkq/focused");
export const onFocusLost = createAction("__rtkq/unfocused");
export const onOnline = createAction("__rtkq/online");
export const onOffline = createAction("__rtkq/offline");
let initialized: Boolean = false;
export function setupListeners(
dispatch: ThunkDispatch<any, any, any>,
customHandler?: (
dispatch: ThunkDispatch<any, any, any>,
actions: {
onFocus: typeof onFocus;
onFocusLost: typeof onFocusLost;
onOnline: typeof onOnline;
onOffline: typeof onOffline;
}
) => () => void
) {
const mobileDefaultHandler = () => {
const handleFocus = () => dispatch(onFocus());
const handleFocusLost = () => dispatch(onFocusLost());
const handleOnline = () => dispatch(onOnline());
const handleOffline = () => dispatch(onOffline());
let unsubscribeAppState: NativeEventSubscription;
let networkSubscription: EventSubscription;
if (!initialized) {
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === "active") {
handleFocus();
} else {
handleFocusLost();
}
};
unsubscribeAppState = AppState.addEventListener(
"change",
handleAppStateChange
);
networkSubscription = addNetworkStateListener(
({ type, isConnected, isInternetReachable }) => {
if (isConnected && isInternetReachable) {
handleOnline();
} else {
handleOffline();
}
console.log(
`Network type: ${type}, Connected: ${isConnected}, Internet Reachable: ${isInternetReachable}`
);
}
);
initialized = true;
}
const unsubscribe = () => {
if (unsubscribeAppState) unsubscribeAppState.remove();
if (networkSubscription) networkSubscription.remove();
initialized = false;
};
return unsubscribe;
};
const webDefaultHandler = () => {
const handleFocus = () => dispatch(onFocus());
const handleFocusLost = () => dispatch(onFocusLost());
const handleOnline = () => dispatch(onOnline());
const handleOffline = () => dispatch(onOffline());
const handleVisibilityChange = () => {
if (window.document.visibilityState === "visible") {
handleFocus();
} else {
handleFocusLost();
}
};
if (!initialized) {
if (typeof window !== "undefined" && window.addEventListener) {
// Handle focus events
window.addEventListener(
"visibilitychange",
handleVisibilityChange,
false
);
window.addEventListener("focus", handleFocus, false);
// Handle connection events
window.addEventListener("online", handleOnline, false);
window.addEventListener("offline", handleOffline, false);
initialized = true;
}
}
const unsubscribe = () => {
window.removeEventListener("focus", handleFocus);
window.removeEventListener("visibilitychange", handleVisibilityChange);
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
initialized = false;
};
return unsubscribe;
};
return customHandler
? customHandler(dispatch, { onFocus, onFocusLost, onOffline, onOnline })
: Platform.OS === "web"
? webDefaultHandler()
: mobileDefaultHandler();
}
```ts |
Beta Was this translation helpful? Give feedback.
-
Problem
I'm currently upgrading and refactoring a legacy react native app to use redux-toolkit and the most up to date react and react-native versions. During this process I've come across some questions regarding rtk's behaviour with react native.
Specifically I have an api slice and a view that needs to have up to date data when opened and periodically updates afterwards when open. Before in rtk 1.8.0 I would use
NavigationEvents
fromreact-navigation
with the following (as a workaround forrefetchOnMountOrArgChange
[1]):and a query that skips if
!isVisible
:This stopped working after upgrading to rtk version 1.9.5 as I am getting a "Cannot refetch a query that has not been started yet" error on first open of the view.
What I've tried and detailed questions
!isUninitialized
to guard the use ofrefetch()
but that results in the same error. I expect this could be a bug or at least unwanted behaviour, as the documentation forisUninitialized
states it is true when Query has not started yet.refetchOnMountOrArgsChange
again did not workrefetch()
, but subsequent onFocus events also result in the same "Cannot refetch a query..." errorDoes anyone in the community have an idea why this behaviour is broken and knows a possible solution?
[1] I suspect on react-native the views remain mounted in the virtual DOM via the navigation history or maybe the view hierarchy in the app is majorly broken, and
refetchOnMountOrArgChange
doesn't work because of that. As I understandrefetchOnFocus
in not supposed to work for internal window switching.Beta Was this translation helpful? Give feedback.
All reactions