Skip to content

withErrorBoundary create unnecessary rerenders #17183

Closed

Description

@OoDeLally

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/react

SDK Version

8.55.0

Framework Version

8.55.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

export const MyScreenWithFallback = Sentry.withErrorBoundary(MyScreen, {
  fallback: FatalErrorFallback,
});

Steps to Reproduce

  1. Run this with dummy components

Expected Result

MyScreenWithFallback does not rerender when props are identical.

Actual Result

MyScreenWithFallback rerenders everytime.

Workaround

export const MyScreenWithFallback = React.memo(Sentry.withErrorBoundary(MyScreen, {
  fallback: FatalErrorFallback,
}));

Real solution

function withErrorBoundary(
  WrappedComponent,
  errorBoundaryOptions,
) {
  const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
  const Wrapped = React.memo(((props) => React.createElement(ErrorBoundary, { ...errorBoundaryOptions,}
      , React.createElement(WrappedComponent, { ...props,} )
    )
  ));

  Wrapped.displayName = `errorBoundary(${componentDisplayName})`;

  // Copy over static methods from Wrapped component to Profiler HOC
  // See: https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
  hoistNonReactStatics(Wrapped, WrappedComponent);
  return Wrapped;
}

Activity

  1. linear commented on Jul 28, 2025

    @linear
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 28, 2025
  3. chargome commented on Jul 28, 2025

    @chargome
    Member

    Hey @OoDeLally would you mind upgrading the SDK to the latest version and check if the issue persists?

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 28, 2025
  5. OoDeLally commented on Jul 28, 2025

    @OoDeLally
    Author

    Hello, thanks for your answer.
    Unfortunately, I am using @sentry/react-native which has a fixed dependency on @sentry/react@8.55.0.
    However I had a look at @sentry/react@9.42.0's dist, and the code looks the same as in 8.55.0, so I assume the behavior would be the same.

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 28, 2025
  7. chargome commented on Jul 28, 2025

    @chargome
    Member

    @OoDeLally thanks for investigating – I'll have a look!

  8. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 28, 2025
  9. self-assigned this
    on Jul 28, 2025
  10. chargome commented on Jul 28, 2025

    @chargome
    Member

    Hey @OoDeLally I had a quick look at this. I did not encounter any unexpected re-renders (testing on react 19) – can you specify what you exactly mean with

    MyScreenWithFallback does not rerender when props are identical.

    Which props are you referring to?

  11. moved this to Waiting for: Community in GitHub Issues with 👀 3on Jul 29, 2025
  12. OoDeLally commented on Jul 29, 2025

    @OoDeLally
    Author

    I use why-did-you-render to monitor rerenderings:

    Without React.memo(...)

    LOG      {"errorBoundary(NounScreen)": [Function Wrapped]} Re-rendered because the props object itself changed but its values are all equal. This could have been avoided by making the component pure, or by preventing its father from re-rendering. More info at http://bit.ly/wdyr02
    LOG      prev props: {"caseId": "Nominative"}  !==  {"caseId": "Nominative"} :next props
    

    With React.memo.(...)

    // No logs
    

    The version with the memo() is noticeably faster when switching screens.

  13. moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 29, 2025
  14. github-actions commented on Jul 31, 2025

    @github-actions
    Contributor

    A PR closing this issue has just been released 🚀

    This issue was referenced by PR #17230, which was included in the 10.0.0 release.

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

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions