0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【覚書】ReactDOM.render がコケたかどうかを取得する

Last updated at Posted at 2021-09-19

React のレンダリングがエラーで失敗したときに
代わりの DOM を表示したいと思ったので調べた

どうやら ReactDOM そのものに
そういうエラーハンドリング機能はなさそうなので、App を細工する。

というわけでこうした

class App extends React.Component {
  constructor() {
    super();
  }
  componentDidCatch(error, errorInfo) {
    // ここにエラー時の処理
    console.log({ error, errorInfo });
  }
  render() {
    return (
      <p>Hello!</p>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

hooks とかはないもよう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?