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.

Combination of componentDidCatch and hooks throws "Should have a queue. This is likely a bug in React."

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

If a component with hooks throws an error in its render function after the hooks have been defined, and that render error is caught via a componentDidCatch in a parent component, any subsequent components will have their hook order jumbled up on the next render.

This results in the app crashing with an “Uncaught Invariant Violation: Should have a queue. This is likely a bug in React. Please file an issue.” error (or different messages depending on the specific hooks used)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/34mvmoln65

(once loaded, open the dev tools console and click the ‘Trigger re-render’ button)

Relevant source:

function App(props) {
  const [, setCounter] = React.useState(0);
  return (
    <div>
      <ErrorHandler>
        <ErrorThrower />
      </ErrorHandler>
      <StatefulComponent />
      <button onClick={() => setCounter(value => value + 1)}>
        Trigger re-render
      </button>
    </div>
  );
}

function ErrorThrower() {
  React.useMemo(() => undefined, []);
  if (true) {
    throw new Error("!!!");
  }
  return <p>[Error component]</p>;
}

function StatefulComponent() {
  React.useState(null);
  return <p>[Stateful component]</p>;
}

class ErrorHandler extends React.Component {
  ...
  componentDidCatch(error) {
    ...
  }
}

What is the expected behavior?

The app should not crash, seeing as the componentDidCatch() ought to catch the render error and allow the rest of the app to render as normal

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

This will presumably affect all versions of React that include the current Hooks implementation (v16.8 onwards)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:8

github_iconTop GitHub Comments

9reactions
2012mjmcommented, Jul 3, 2019

I need the error handling with componentDidCatch feature in the hook. Is there a alternate for error handling?

3reactions
Jokcycommented, Apr 3, 2019

I guess I find the reason of this bug. While ErrorThrower throw the error, the renderWithHooks function break down, so we can’t reach the line of code workInProgressHook = null reset after we finish render FunctionComponent. So the workInProgressHook stay useMemo when render StatefulComponent.

Maybe I can do a PR to fix this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Should have a queue. This is likely a bug in React ...
I am using react-i18next . Sometime on init function component facing React issue. Any idea what may causing it? My config import i18n...
Read more >
Error Boundaries - React
A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch() .
Read more >
Troubleshooting common React Native bugs - LogRocket Blog
This troubleshooting guide shares how to easily find and address common React Native bugs. See common error examples.
Read more >
react-error-boundary - npm
The simplest way to use <ErrorBoundary> is to wrap it around any component that may throw an error. This will handle errors thrown...
Read more >
Failing Gracefully with React Error Boundary - YouTube
Courses - https://learn.codevolution.dev/ Support UPI - https://support.codevolution.dev/ Support PayPal ...
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