question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`stopPolling` doesn't work when using React StrictMode

See original GitHub issue

Intended 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:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
brioshejecommented, Oct 20, 2021

This is probably fixed in the 3.5, if you want to try it (it’s currently in a release candidate stage).

I think there is an info missing in the documentation about the fact that pollInterval and start/stop cannot be used together, hence I’ve found this working solution:

Yeah I’ve found it weird that you essentially pass a new pollInterval via the function, whereas I expected the pollInterval() passed to the original hook to be the source of truth, and the functions to take no arguments.

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 😃

2reactions
brioshejecommented, Oct 19, 2021

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 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 and start/stop cannot be used together, hence I’ve found this working solution:

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. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found