-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
stopPolling
doesn't work when using React StrictMode
#7221
Comments
I noticed that stopPolling works only if the polling is started using startPolling (so without passing pollIntervall options in query declaration). Is this the desired behaviour?
|
@gius80 If you set a breakpoint (or |
@benjamn In both cases this.reobserver is not undefined, is a valid object:
I see that pollInterval in |
@gius80 We do have a passing test of calling itAsync('should stop polling on unmount using stopPolling', (resolve, reject) => {
let renderCount = 0;
const Component = () => {
let { data, loading, stopPolling } = useQuery(CAR_QUERY, {
pollInterval: 100,
});
useEffect(() => stopPolling, []);
switch (++renderCount) {
case 1:
expect(loading).toBeTruthy();
break;
case 2:
expect(loading).toBeFalsy();
expect(data).toEqual(CAR_RESULT_DATA);
unmount();
break;
default:
reject(new Error('Uh oh - we should have stopped polling!'));
}
return null;
};
const { unmount } = render(
<MockedProvider link={new MockLink(CAR_MOCKS)}>
<Component />
</MockedProvider>
);
return wait(() => {
expect(renderCount).toBe(2);
}).then(resolve, reject);
}); Can you see anything I'm missing in this test, compared to what you're doing? |
@benjamn test code seems ok. I published a very simple example to illustrate the issue (it's a default react app). http://apollo-polling.s3-website-eu-west-1.amazonaws.com/
Here is the code: index.tsx
App.tsx
|
Any update on this?, I have the exact same issue (3.3.8v) - weird thing is that I have setup locally apollo library (same version) in development mode with -> https://github.com/apollographql/apollo-client/blob/main/CONTRIBUTING.md this fullstack setup guide. And the problem haven't occured, just gave up cause with |
Also running into this issue as well ! |
Same issue here. Any updates? |
Seeing the same issue, this makes polling difficult to achieve because it seems like the only way to use it is unmountng the component with the query |
Had this issue (and a similar issue where the polling continued on other pages despite being unmounted) after upgrading apollo and CRA on development but not production and I traced it to |
stopPolling
doesn't work when using React StrictMode
I’m only able to reproduce this bug when using React.StrictMode, so I updated the title. If anyone can come up with a reproduction that doesn’t use strict mode let me know. |
I'm still seeing this in v3.4.10. I'm using this as a workaround: const { data, loading, client } = useQuery(GetData, {
pollingInterval: 5000
})
useEffect(() => {
return () => {
client.stop()
}
}, [client]); |
Stilll experiencing this in 2021 (late october), but the above doesn't work for me because using I think there is an info missing in the documentation about the fact that const { data, loading, startPolling, stopPolling } = useQuery(GetData);
useEffect(() => {
startPolling(desiredTimeoutInMilliseconds);
}, [startPolling]);
useEffect(() => {
return () => stopPolling();
}, [stopPolling]); Summarized, when the component is mounted the polling will start. This seems to consistently work, hence I'm keeping this solution, hope this helps someone. |
This is probably fixed in the 3.5, if you want to try it (it’s currently in a release candidate stage).
Yeah I’ve found it weird that you essentially pass a new pollInterval via the function, whereas I expected the |
Either way, I think the cleverest would be to have the hook to automatically stop the autopolling when the component is destroyed, since the polling is probably intended to be linked to the mounting component. I will give a try to the RC when I happen to use that specific scenario again, right now I'm pretty good with this solution :) |
Still experiencing this issue with a React Native project. We are using start/stopPolling (not pollInterval) and while start works, stop appears to do nothing. |
I've used the approach I've provided above in react native as well and didn't have any issue, are you sure you are correctly invoking the stop method? |
I work with @branaust - this is the code in question (simplified a bit) const { data, loading, startPolling, stopPolling } = useQuery(
GQL_QUERY,
{
variables: { id },
}
);
const isItemPending = data?.items.some(
(item) => item.status !== "READY"
);
useEffect(() => {
isItemPending ? startPolling(60 * 1000) : stopPolling();
}, [isVideoPending]);
useEffect(() => stopPolling, []); Start works as expected but stop never works either on unmount or in the event that there are no pending items. |
Its almost 2023 and still the behaviour remains the same, |
This apollo issue with leaked polling caused a major problem for us this week. Thanks @briosheje, your answer saved us a lot of trouble. |
Intended outcome:
According to documentation, stopPolling function should stop the query polling.
Actual outcome:
After calling stopPolling(), polling continues.
How to reproduce the issue:
Versions
System:
OS: macOS 10.15.6
Binaries:
Node: 14.12.0 - /usr/local/bin/node
Yarn: 1.22.5 - /usr/local/bin/yarn
npm: 6.14.8 - /usr/local/bin/npm
Browsers:
Chrome: 86.0.4240.111
Firefox: 81.0.2
Safari: 14.0
npmPackages:
@apollo/client: ^3.2.5 => 3.2.5
The text was updated successfully, but these errors were encountered: