onCompleted is called during render phase, causing React errors (reproduction provided)
See original GitHub issueIntended outcome: There should be no React errors when using apollo client
Actual outcome: React error is printed to the console:
Warning: Cannot update a component (`App`) while rendering a different component (`PeopleList`). To locate the bad setState() call inside `PeopleList`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
The cause of this seems to be that onCompleted
gets called during the render phase if the query results are available in the cache. This can cause problems when doing something like dispatching a redux action from inside the onCompleted
function.
How to reproduce the issue:
Clone and run my reproduction, using branch react-console-errors
: https://github.com/dylanwulf/react-apollo-error-template/tree/react-console-errors. Install packages with npm install
and run the app with npm start
. When running the app, wait until “parent query completed” is true, then click the button and watch the console log.
Versions
System:
OS: Windows 10 10.0.22000 (envinfo doesn't seem to detect this properly, I'm actually running Windows 11)
Binaries:
Node: 16.15.0 - C:\Program Files\nodejs\node.EXE
npm: 8.5.5 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22000.120.0), Chromium (102.0.1245.33)
npmPackages:
@apollo/client: ^3.6.6 => 3.6.6
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Error Boundaries - React
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
Read more >Reactjs How value is passed using onCompleted() in formik
onCompleted() is called in useMutation because i want to open=true (open is a flag) when mutation is done than this flag value should...
Read more >8 common React error messages and how to address them
Learn about the most common error messages in React Development and the meaning behind them, the error itself, and how to fix it....
Read more >useQuery | TanStack Query Docs
useQuery · Defaults to the global query config's useErrorBoundary value, which is undefined · Set this to true if you want errors to...
Read more >Don't call a React function component - Kent C. Dodds
Warning: React has detected a change in the order of Hooks called by BadCounterList. This will lead to bugs and errors if not...
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
Fixed by #9801!
@simplecommerce I can confirm that the reproduction you provided does get fixed with the PR I submitted