apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

useQuery `pollInterval` stop working in React v18 StrictMode

Open pobch opened this issue 2 years ago • 6 comments

Intended outcome: pollInterval should work either with React v18 StrictMode or without StrictMode

Actual outcome: pollInterval stop working when using it with React v18 StrictMode

How to reproduce the issue:

  1. Go to https://codesandbox.io/s/heuristic-bohr-df8gf9?file=/src/index.js
  2. Open the dev tools, go to network tab
  3. BUG: There is no new request to the graphql endpoint even though the pollingInterval is set to 2000
  4. If you remove <React.StrictMode> in index.js file, the polling will work as expected.

pobch avatar Jun 14 '22 11:06 pobch

+1 Seeing the same issue and confirmed that step 4 worked, but not an ideal solution

cdnicoll avatar Jun 22 '22 02:06 cdnicoll

Experiencing this as well. Manually triggering the poll via startPolling is a workaround

boyerst avatar Jun 22 '22 19:06 boyerst

I am experiencing this as well.

bentron2000 avatar Jul 14 '22 23:07 bentron2000

Same here!

martentamm avatar Jul 18 '22 13:07 martentamm

+1!

gaswirth avatar Aug 05 '22 14:08 gaswirth

Experiencing the same.

vedmakk avatar Aug 30 '22 12:08 vedmakk

Same here 🥲

Maxou44 avatar Oct 31 '22 20:10 Maxou44

Same here, I have to pass refetch around. Any idea if this will be fixed soon?

Zonalds avatar Nov 01 '22 12:11 Zonalds

Same here too! 😢

thiagokpelo avatar Nov 02 '22 15:11 thiagokpelo

Same, in my case its not working even if i call startPolling manually in useEffect. I have also set reactStrictMode to false in next.config.js and it doesnt solve the issue

angelhodar avatar Nov 02 '22 15:11 angelhodar

If I set reactStrictMode to false in next.config.js, it works. And the workaround with startPolling and stopPolling also works.

thiagokpelo avatar Nov 02 '22 15:11 thiagokpelo

@thiagokpelo Mmmm then i dont understand why it doesnt work in my case. I should try a minimal reproduction example. Thanks for telling me!!

angelhodar avatar Nov 02 '22 15:11 angelhodar

@angelhodar will update us here if it resolves. If I can help just say! 👊

thiagokpelo avatar Nov 02 '22 19:11 thiagokpelo

@thiagokpelo I dont know what i am doing wrong but with a fresh nextjs project and the last version of apollo client i cant get the poll interval to work. What versions did you use to debug your nextjs project? I have tried to manually poll with startPolling and disable reactStrictMode to false and still doesnt work

angelhodar avatar Nov 02 '22 19:11 angelhodar

Same issue here. Next js with strict mode disabled and still not working

jpmermoz avatar Nov 03 '22 00:11 jpmermoz

@angelhodar I'm using


 "dependencies": {
    "@apollo/client": "^3.7.0",
    "@chakra-ui/react": "^2.3.5",
    "@datadog/browser-logs": "^4.21.2",
    "@datadog/browser-rum": "^4.21.2",
    "@emotion/react": "^11",
    "@emotion/styled": "^11",
    "@react-oauth/google": "^0.2.8",
    "apollo-link-rest": "^0.9.0-rc.1",
    "framer-motion": "^6",
    "graphql": "^16.6.0",
    "graphql-ws": "^5.11.2",
    "next": "12.3.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-intl": "^6.1.2"
  }

Have you tried to run the built project and with npm start?

thiagokpelo avatar Nov 03 '22 14:11 thiagokpelo

Same issue here. Maybe it is dedicated to local Nextjs server yarn dev, because my app on Vercel seems fine. Manually startPolling works too.

siegfried avatar Nov 20 '22 20:11 siegfried

We are experiencing the same issue at https://github.com/ExpressLRS/ExpressLRS-Configurator

jurgelenas avatar Dec 02 '22 06:12 jurgelenas

I had the same issue using @apollo/client:3.7.1 and this new 3.7.2 version using CRA (React version 18.0.0).

Using a timer fixed the issue temporary.

Example of the fix:

const { refetch } = useMyEntity({
    onCompleted(data) {
       // Snip
    });
   // snip
   useEffect(() => {
    const refetchQueryInterval = setInterval(() => {
      refetch();
    }, 5000;
    return () => clearInterval(refetchQueryInterval);
});

MrDesjardins avatar Dec 16 '22 19:12 MrDesjardins

I stumbled in this issue as well. Specially with Next.JS since it enables strict mode by default (go to next.config.js and turn the flag off).

afonsomatos avatar Dec 25 '22 12:12 afonsomatos

The same issue with Next.js 13.0.0 I set reactStrictMode to false but that does not help

IShinkarenko avatar Dec 28 '22 11:12 IShinkarenko

@IShinkarenko You probably did that but make sure you restart (shutdown and start) the Next.JS server. Changing next.config.js won't have an effect on an ongoing instance, even if started with next dev.

afonsomatos avatar Jan 04 '23 18:01 afonsomatos

Similar issue with @apollo/client@^3.7.3 and [email protected] (just updated) next start works as expected; next dev fails to start polling.

Using the following temp fix as others have suggested:

const {
  data: indexQuery,
  error: indexError,
  startPolling,
} = useQuery(IndexDocument, {
  ssr: false,
  // The following line is removed
  // pollInterval: 500,
});

// The following useEffect is added
useEffect(() => {
  startPolling(500);
}, [startPolling]);

ramblehead avatar Jan 06 '23 15:01 ramblehead

I tried version 3.7.4 but this seems to still be an issue - is they any idea as when this will be fixed ?

MarkAPhillips avatar Jan 16 '23 13:01 MarkAPhillips

[email protected] and @apollo/[email protected] , still not working, any news? for me is not working with useQuery, useLazyQuery, with reactStrictMode: false in next.config.js - next start / next dev same result

mysticek avatar Feb 27 '23 08:02 mysticek

@mysticek I think this is closely related to https://github.com/apollographql/apollo-client/issues/10540#issuecomment-1456300209 the workaround suggested there worked for polling too (which is similar to what @ramblehead did)

Hopefully, we find a good way to fix this soon 😊

patrick91 avatar Mar 07 '23 19:03 patrick91

This is fixed in #10629 and will hopefully land in the next patch release.

phryneas avatar Mar 08 '23 14:03 phryneas

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.

github-actions[bot] avatar Apr 08 '23 00:04 github-actions[bot]