Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
calebmer opened this issue May 24, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@calebmer
Copy link
Contributor

commented May 24, 2019

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:

// The following should have already been reset
// currentHook = null;
// workInProgressHook = null;
// remainingExpirationTime = NoWork;
// componentUpdateQueue = null;
// didScheduleRenderPhaseUpdate = false;
// renderPhaseUpdates = null;
// numberOfReRenders = 0;
// sideEffectTag = 0;

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.

@calebmer

This comment has been minimized.

Copy link
Contributor Author

commented May 27, 2019

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!

@gaearon

This comment has been minimized.

Copy link
Member

commented May 27, 2019

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

Or maybe resetHooks doesn't reset everything we need.

calebmer added a commit to calebmer/react that referenced this issue May 27, 2019

@calebmer

This comment has been minimized.

Copy link
Contributor Author

commented May 27, 2019

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.

@calebmer calebmer closed this May 27, 2019

gaearon added a commit that referenced this issue May 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.