-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactErrorBoundries.js
More file actions
50 lines (41 loc) · 1.44 KB
/
ReactErrorBoundries.js
File metadata and controls
50 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*Error Boundaries are a feature in React that allows components to catch JavaScript errors anywhere in their tree of child components and log those errors,
display a fallback UI, or take any other appropriate action*/
import React, { Component } from 'react';
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
// This method is called when an error occurs in the components inside ErrorBoundary
componentDidCatch(error, errorInfo) {
this.setState({ hasError: true });
// Log the error details to the console (you can customize this part)
console.error('Error caught by boundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
// Render a friendly error message when an error occurs
return <h1>Something went wrong. Please try again later.</h1>;
}
// Render the child components as usual
return this.props.children;
}
}
// Example component that may throw an error
class YourComponent extends Component {
render() {
// Simulate an error for demonstration purposes
throw new Error('This is a simulated error in YourComponent.');
// Render your actual component content here
return <div>Your Component Content</div>;
}
}
// Wrap YourComponent with ErrorBoundary to catch and handle errors
function App() {
return (
<ErrorBoundary>
<YourComponent />
</ErrorBoundary>
);
}
export default App;