Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useSubscription bug in React v18 StrictMode (#9664) #9707

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Guarantee `Concast` cleanup without `Observable cancelled prematurely` rejection, potentially solving long-standing issues involving that error. <br/>
[@benjamn](https://github.com/benjamn) in [#9701](https://github.com/apollographql/apollo-client/pull/9701)

- Ensure `useSubscription` subscriptions are properly restarted after unmounting/remounting by React 18 in `<StrictMode>`. <br/>
[@benjamn](https://github.com/benjamn) in [#9707](https://github.com/apollographql/apollo-client/pull/9707)

### Improvements

- Internalize `useSyncExternalStore` shim, for more control than `use-sync-external-store` provides, fixing some React Native issues. <br/>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.min.cjs",
"maxSize": "29.45kB"
"maxSize": "29.5kB"
}
],
"engines": {
Expand Down
27 changes: 18 additions & 9 deletions src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
});
});

const canResetObservableRef = useRef(false);
useEffect(() => {
return () => {
canResetObservableRef.current = true;
};
}, []);

const ref = useRef({ client, subscription, options });
useEffect(() => {
let shouldResubscribe = options?.shouldResubscribe;
Expand All @@ -46,23 +53,24 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
}

if (options?.skip) {
if (!options?.skip !== !ref.current.options?.skip) {
if (!options?.skip !== !ref.current.options?.skip || canResetObservableRef.current) {
setResult({
loading: false,
data: void 0,
error: void 0,
variables: options?.variables,
});
setObservable(null);
canResetObservableRef.current = false;
}
} else if (
shouldResubscribe !== false && (
client !== ref.current.client ||
subscription !== ref.current.subscription ||
options?.fetchPolicy !== ref.current.options?.fetchPolicy ||
!options?.skip !== !ref.current.options?.skip ||
!equal(options?.variables, ref.current.options?.variables)
)
(shouldResubscribe !== false &&
(client !== ref.current.client ||
subscription !== ref.current.subscription ||
options?.fetchPolicy !== ref.current.options?.fetchPolicy ||
!options?.skip !== !ref.current.options?.skip ||
!equal(options?.variables, ref.current.options?.variables))) ||
canResetObservableRef.current
) {
setResult({
loading: true,
Expand All @@ -76,10 +84,11 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
fetchPolicy: options?.fetchPolicy,
context: options?.context,
}));
canResetObservableRef.current = false;
}

Object.assign(ref.current, { client, subscription, options });
}, [client, subscription, options]);
}, [client, subscription, options, canResetObservableRef.current]);

useEffect(() => {
if (!observable) {
Expand Down