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.

Entire app remounts on error

See original GitHub issue

Bug report

Describe the bug

I’m not sure if this is intentional or not, but it seems undesirable at least. This is in production mode.

When an error occurs during rendering, triggering Next.js’ error boundary, the entire <App> component remounts. This is counter to what I think the developer’s expectation is, which is that <App> is only mounted once and remains mounted for the entire duration.

I can’t think of a good reason this would be intentional. It seems more likely that the tree above <App> changes in some incidental way, such that React can no longer update the existing element instances but remounts them instead. For example:

<SomeContainer>                 <OtherContainer>
  <App>                           <App>
    <Page />   -- on error -->      <Page />
  </App>                          </App>
</SomeContainer>                </OtherContainer>

or:

                              <ErrorContainer>
<App>                           <App>  
  <Page />   -- on error -->      <Page />
</App>                          </App>
                              </ErrorContainer>

The downside is that code people expected to mount only once (potentially expensive initialization, recreating things that don’t need to be recreated, etc.) can happen multiple times.

To Reproduce

This repo demonstrates the issue: https://github.com/exogen/next-error-remount

Clicking the button will logs what is being unmounted.

Expected behavior

Transition to the error state should be just like transitioning pages: the <Page> should unmount and the <Error> should mount, but the <App> should remain mounted.

System information

  • Version of Next.js: 8.0.4, 8.1.0, canary

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ericbiewenercommented, Sep 12, 2022

Nevermind, an <ErrorBoundary> does successfully catch the error. See the with-error-boundary branch here.

Therefore, everything appears to work correctly in my opinion. I don’t ultimately agree with the original post that <App> should not unmount when an error in a component’s render method occurs. This isn’t really a Next.js decision, but just how React works – everything up to an ErrorBoundary will unmount. It can’t just keep the same <App> mounted with the same props because that will just cause the error to get thrown again.

1reaction
Benimationcommented, Jun 8, 2021

This is especially fun when the error is caused by an effect in App that’s only supposed to run on mount… Creating an infinite loop…

The solution, of course, is to handle the error, but this was indeed very unexpected…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entire app remounts on error · Issue #7578 · vercel/next.js
When an error occurs during rendering, triggering Next.js' error boundary, the entire <App> component remounts.
Read more >
React Router remounts component everytime - Stack Overflow
I can see the logging from renderForm only when the App component is mounted and not when I switch between routes. However, I...
Read more >
Error Boundary Remounting - YouTube
Chapters. View all · Intro · Intro · Intro · Error · Error · Error · Remount · Remount.
Read more >
Root error: Can't remount /system | XDA Forums
Hello, I got an OP3 with latest Open Beta installed. It is rooted via SuperSU ZIP. Most root apps work, for example "Root...
Read more >
Catching Errors in React with Error Boundaries
Whenever an error occurs, the entire sub-tree of the error boundary is unmounted, which in turn will reset any internal state. Providing the ......
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