diff --git a/_posts/2017-07-26-error-handling-in-react-16.md b/_posts/2017-07-26-error-handling-in-react-16.md index d739c53b..574716ad 100644 --- a/_posts/2017-07-26-error-handling-in-react-16.md +++ b/_posts/2017-07-26-error-handling-in-react-16.md @@ -32,7 +32,7 @@ class ErrorBoundary extends React.Component { // You can also log the error to an error reporting service logErrorToMyService(error, info); } - + render() { if (this.state.hasError) { // You can render any custom fallback UI @@ -75,6 +75,18 @@ For example, Facebook Messenger wraps content of the sidebar, the info panel, th We also encourage you to use JS error reporting services (or build your own) so that you can learn about unhandled exceptions as they happen in production, and fix them. +## Component Stack Traces + +React 16 prints all errors to the console in development, even if the application accidentally swallows them. In addition to the error message and the JavaScript stack, it also provides component stack traces. Now you can see where exactly in the component tree the failure has happened: + +Component stack traces in error message + +You can also see the filenames and line numbers in the component stack trace. This works by default in [Create React App](https://github.com/facebookincubator/create-react-app) projects: + +Component stack traces with line numbers in error message + +If you don’t use Create React App, you can add [this plugin](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) manually to your Babel configuration. Note that it’s intended only for development and **must be disabled in production**. + ## Why Not Use `try` / `catch`? `try` / `catch` is great but it only works for imperative code: diff --git a/img/blog/error-boundaries-stack-trace-line-numbers.png b/img/blog/error-boundaries-stack-trace-line-numbers.png new file mode 100644 index 00000000..b8a7abcb Binary files /dev/null and b/img/blog/error-boundaries-stack-trace-line-numbers.png differ diff --git a/img/blog/error-boundaries-stack-trace.png b/img/blog/error-boundaries-stack-trace.png new file mode 100644 index 00000000..cf27be3d Binary files /dev/null and b/img/blog/error-boundaries-stack-trace.png differ