Create error boundary components
✓Works with OpenClaudeYou are a React component specialist. The user wants to create error boundary components that catch JavaScript errors in child components and display fallback UI.
What to check first
- Verify React version is 16.8+ (
npm list react— error boundaries require class components) - Check if you're using a class component (error boundaries must be class-based, not functional)
- Ensure the error boundary wraps components that might throw, not the throwing component itself
Steps
- Create a class component extending
React.Component(not a functional component) - Implement the
static getDerivedStateFromError(error)method to update state when an error is caught - Implement the
componentDidCatch(error, errorInfo)method for side effects like logging - Maintain an
hasErrorboolean in state to conditionally render fallback UI - Render children normally if
hasErroris false, otherwise render your fallback UI - Wrap error-prone components with your error boundary in the parent component tree
- Test by intentionally throwing an error in a child component to verify the boundary catches it
- Log errors to an external service in
componentDidCatchfor production monitoring
Code
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null, errorInfo: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
this.setState({ errorInfo });
// Log to error reporting service
console.error('Error caught by boundary:', error, errorInfo);
// Example: logErrorToService(error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div style={{ padding: '20px', border: '1px solid red', borderRadius: '4px' }}>
<h2 style={{ color: 'red' }}>Something went wrong</h2>
<details style={{ whiteSpace: 'pre-wrap', marginTop: '10px' }}>
{this.state.error && this.state.error.toString()}
<br />
{this.state.errorInfo && this.state.errorInfo.componentStack}
</details>
</div>
);
}
return this.props.children;
}
}
// Usage example:
function BuggyComponent() {
throw new Error('This component broke!');
}
export default function App() {
return (
<ErrorBoundary>
<h1>My App</h1>
<BuggyComponent />
</ErrorBoundary>
);
}
Pitfalls
- Error boundaries only catch errors in the render phase and lifecycle methods — they don
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related General / Utility Skills
Other Claude Code skills in the same category — free to download.
Env Setup
Set up development environment from scratch
Gitignore Generator
Generate .gitignore for any project type
NPM Publish
Prepare and publish npm package
Feature Flag
Implement feature flag system
Config Manager
Create application configuration manager
Date Time Helper
Create date/time utility functions
String Utils
Create string manipulation utilities
File Utils
Create file system utility functions
Want a General / Utility skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.