Question
How can I provide more context about the error using `errorInfo`?
Asked by: USER6837
65 Viewed
65 Answers
Answer (65)
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
}
```