-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
fix: Properly wrap components in traditional HoC pattern #13605
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,32 @@ import React from 'react'; | |
import RouteError from 'app/views/routeError'; | ||
|
||
export default function errorHandler(Component) { | ||
const originalRender = Component.prototype.render; | ||
Component.prototype.render = function() { | ||
try { | ||
return originalRender.apply(this, arguments); | ||
} catch (err) { | ||
/*eslint no-console:0*/ | ||
class ErrorHandler extends React.Component { | ||
static getDerivedStateFromError(error) { | ||
setTimeout(() => { | ||
throw err; | ||
throw error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm wonder if we even need this since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. I don't think there's anything catching it - I think we only use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I'll follow up with a PR to remove it. |
||
}); | ||
return <RouteError error={err} component={this} />; | ||
|
||
// Update state so the next render will show the fallback UI. | ||
return { | ||
hasError: true, | ||
error, | ||
}; | ||
} | ||
|
||
state = { | ||
hasError: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a bit redundant? We know error state with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially thought that the Here's a sample react app showcasing that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, that's an additional issue, we should never be throwing |
||
error: null, | ||
}; | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return <RouteError error={this.state.error} />; | ||
} | ||
|
||
return <Component {...this.props} />; | ||
} | ||
}; | ||
return Component; | ||
} | ||
|
||
return ErrorHandler; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, interesting we were doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evanpurkhiser it's super old-school React 😄 see: preactjs/preact#531 (comment)