Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Fixed unmounted component state update #4021

Open
wants to merge 1 commit into
base: release-3.x
Choose a base branch
from
Open
Changes from all 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
16 changes: 14 additions & 2 deletions packages/hooks/src/utils/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function useBaseQuery<TData = any, TVariables = OperationVariables>(
lazy = false
) {
const context = useContext(getApolloContext());
const isMounted = useRef(false);
const [tick, forceUpdate] = useReducer((x) => x + 1, 0);
const updatedOptions = options ? { ...options, query } : { query };

Expand All @@ -31,7 +32,13 @@ export function useBaseQuery<TData = any, TVariables = OperationVariables>(
// force a re-render to make sure the new data is displayed. We can't
// force that re-render if we're already rendering however so to be
// safe we'll trigger the re-render in a microtask.
Promise.resolve().then(forceUpdate);
Promise.resolve().then(() => {
if (!isMounted.current) {
return;
}

return forceUpdate();
});
} else {
// If we're rendering on the server side we can force an update at
// any point.
Expand Down Expand Up @@ -75,7 +82,12 @@ export function useBaseQuery<TData = any, TVariables = OperationVariables>(
queryDataRef.current = queryData;
}

return () => queryData.cleanup();
isMounted.current = true;

return () => {
isMounted.current = false;
return queryData.cleanup();
};
}, []);

useEffect(() => queryData.afterExecute({ lazy }), [
Expand Down