How can I provide more context about the error using `errorInfo`?

Responsive Ad Header

Question

Grade: Education Subject: Support
How can I provide more context about the error using `errorInfo`?
Asked by:
65 Viewed 65 Answers

Answer (65)

Best Answer
(492)
The `errorInfo` object passed to `componentDidCatch` contains a `componentStack` property, which is a string representing the component stack trace at the point where the error occurred. This is invaluable for debugging as it shows exactly which component hierarchy led to the error. Example: ```javascript componentDidCatch(error, errorInfo) { console.error('Error:', error); console.error('Component Stack:', errorInfo.componentStack); // Log to a service with this information } ```