You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
“Rendered fewer hooks than expected” fired in an error boundary component without hooks #15732
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:
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.
importReact,{useState,useEffect}from"react";import{render}from"react-dom";functionApp(){return(<ErrorBoundary><Thrower/></ErrorBoundary>);}classErrorBoundaryextendsReact.Component{state={hasError: false};staticgetDerivedStateFromError(){return{hasError: true};}render(){return(<Wrapper>{this.state.hasError ? null : this.props.children}</Wrapper>);}}functionThrower(){const[shouldThrow,setShouldThrow]=useState(false);if(shouldThrow){thrownewError("Throw!");}useEffect(()=>{},[]);return<buttononClick={()=>setShouldThrow(true)}>Throw!</button>;}functionWrapper({ children }){returnchildren;}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
Activity
calebmer commentedon May 27, 2019
I ran into the issue again and was able to produce a small repro: https://codesandbox.io/s/intelligent-cannon-f6bqx
Pressing the “throw” button results in the invariant:
With a component stack of:
Which is weird since
<Wrapper>has no hooks!gaearon commentedon May 27, 2019
Wanna look into fixing? Should be easy — there must be a missing
resetHookscall somewhere.Or maybe
resetHooksdoesn't reset everything we need.Adds test for react#15732.
calebmer commentedon 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.
Adds test for #15732. (#15747)
TedDriggs commentedon Sep 3, 2019
I thought I was seeing this in 16.9, but it turns out we were on
react@16.9.0andreact-dom@16.8.6, and both need to be updated for this to work.Adds test for react#15732. (react#15747)