Skip to content

“Rendered fewer hooks than expected” fired in an error boundary component without hooks #15732

Closed

Description

@calebmer

I’ve seen the “rendered fewer hooks than expected” invariant a couple times now pointing at a component rendered as a part of an error boundary that just caught an error when that component does not use hooks at all.

I’ve been able to fix this issue by un-commenting:

https://github.com/facebook/react/blob/8af1f879299e88f9e370c1f01d90efaeefcefd27/packages/react-reconciler/src/ReactFiberHooks.js#L372-L382

I haven’t been able to produce a minimal test-case yet 😣. I’m reporting anyway with the temporary fix that worked for me in case you have any insight on what a minimal test-case could be. I’ll update this issue if I keep running into the problem and I can create a minimal test-case.

Activity

  1. calebmer commented on May 27, 2019

    @calebmer
    ContributorAuthor

    I ran into the issue again and was able to produce a small repro: https://codesandbox.io/s/intelligent-cannon-f6bqx

    import React, { useState, useEffect } from "react";
    import { render } from "react-dom";
    
    function App() {
      return (
        <ErrorBoundary>
          <Thrower />
        </ErrorBoundary>
      );
    }
    
    class ErrorBoundary extends React.Component {
      state = { hasError: false };
    
      static getDerivedStateFromError() {
        return { hasError: true };
      }
    
      render() {
        return (
          <Wrapper>{this.state.hasError ? null : this.props.children}</Wrapper>
        );
      }
    }
    
    function Thrower() {
      const [shouldThrow, setShouldThrow] = useState(false);
    
      if (shouldThrow) {
        throw new Error("Throw!");
      }
    
      useEffect(() => {}, []);
    
      return <button onClick={() => setShouldThrow(true)}>Throw!</button>;
    }
    
    function Wrapper({ children }) {
      return children;
    }
    
    render(<App />, document.getElementById("root"));

    Pressing the “throw” button results in the invariant:

    Uncaught Invariant Violation: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.
        at invariant (https://f6bqx.codesandbox.io/node_modules/react-dom/cjs/react-dom.development.js:55:15)
        at renderWithHooks (https://f6bqx.codesandbox.io/node_modules/react-dom/cjs/react-dom.development.js:13010:28)
    

    With a component stack of:

    The above error occurred in the <Wrapper> component:
        in Wrapper (created by ErrorBoundary)
        in ErrorBoundary (created by App)
        in App
    

    Which is weird since <Wrapper> has no hooks!

  2. gaearon commented on May 27, 2019

    @gaearon
    Collaborator

    Wanna look into fixing? Should be easy — there must be a missing resetHooks call somewhere.

    Or maybe resetHooks doesn't reset everything we need.

  3. added a commit that references this issue on May 27, 2019
  4. calebmer commented on May 27, 2019

    @calebmer
    ContributorAuthor

    I looked into it, wrote a test, and discovered it’s fixed on master 🙂

    The test is broken on 16.8.6, I didn’t bother bisecting to find the commit that fixed it. PR for the test: #15747.

  5. added a commit that references this issue on May 28, 2019
  6. TedDriggs commented on Sep 3, 2019

    @TedDriggs

    I thought I was seeing this in 16.9, but it turns out we were on react@16.9.0 and react-dom@16.8.6, and both need to be updated for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions