`stopPolling` doesn't work when using React StrictMode
See original GitHub issueIntended outcome:
According to documentation, stopPolling function should stop the query polling.
Actual outcome:
After calling stopPolling(), polling continues.
How to reproduce the issue:
export default function MyComponent() {
const {
data: carsData,
loading: carsLoading,
error: carsError,
fetchMore,
stopPolling,
startPolling
} = useQuery<getCars, getCarsVariables>(GET_CARS, {
fetchPolicy: 'network-only',
pollInterval: 10000
})
useEffect(() => {
return () => {
stopPolling() // Doesn't stop polling on unmount
}
}, [])
return (
<>
<Button
onClick={() => {
stopPolling() // Doesn't stop polling on click
}}
>
STOP
</Button>
{/* MyComponent Render Stuff */}
</>
)
}
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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:19 (4 by maintainers)
Top Results From Across the Web
Strict Mode - React
Strict mode checks are run in development mode only; they do not impact the production build. You can enable strict mode for any...
Read more >Does strict mode work differently with React 18?
Strict Mode is a tool that helps identify coding patterns that may cause problems when working with React, like impure renders.
Read more >Consuming an Apollo GraphQL Server using React
This is an introduction to how we can interact with an Apollo GraphQL server from the frontend. If you are not familiar with...
Read more >Wait, you're not using <React.StrictMode>?! - Medium
The example above shows Time slicing and scheduling, using it in Synchronous mode is the way React works now and using it in...
Read more >How to start and stop polling in React and Livewire - Freek.dev
Don't bother trying to use that token. It is randomly generated, based on some secret user data, and has a very short lifespan....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😃
Stilll experiencing this in 2021 (late october), but the above doesn’t work for me because using
client
stops all the polling, while I just want to stop one specifically.I think there is an info missing in the documentation about the fact that
pollInterval
andstart/stop
cannot be used together, hence I’ve found this working solution:Summarized, when the component is mounted the polling will start. Once the component is destroyed, the polling will be stopped.
This seems to consistently work, hence I’m keeping this solution, hope this helps someone.