Error handling in React can be achieved using Error Boundaries.
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
To create an error boundary, you’ll need to define a new component with either (or both) of the lifecycle methods `getDerivedStateFromError()` or `componentDidCatch()`.
Here is an example of how to use it:
```
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
Then you can use it as a regular component:
```
```
This will catch any error in the `MyWidget` component and its children.
It’s important to note that error boundaries only catch errors in the components below them in the tree. An error boundary can’t catch an error within itself.