Code ReviewAdvanced~18 min

Review: Error Boundary Issues

typescriptreacterror-handlingerror-boundary

Code Review Task

Please review the following React + TypeScript code.

Background

This is an implementation of an Error Boundary component. There are issues with error handling and user experience.

Review Points

  • componentDidCatch implementation
  • Error information logging
  • Appropriateness of fallback UI
  • Error recovery functionality

Your Answer

ErrorBoundary.tsxtypescript
import React, { Component, ReactNode } from 'react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
errorMessage: string;
}
class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
hasError: false,
errorMessage: ''
};
}
componentDidCatch(error: Error) {
this.setState({
hasError: true,
errorMessage: error.message
});
console.log('Error occurred:', error);
}
render() {
if (this.state.hasError) {
return (
<div>
<h1>Something went wrong</h1>
<p>Error: {this.state.errorMessage}</p>
<pre>{this.state.errorMessage}</pre>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;
行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)

Minimum 100 characters required

0

Add comments and enter at least 100 characters in the summary

Model Answer

Submit your answer or click "Show" to view the model answer.