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 5 years have passed since last update.

【Next.js】setStateでハマった(Maximum update depth exceeded)

0
Last updated at Posted at 2020-12-29

前提としての状況

Next.jsを用いてポートフォリオサイトを制作中
自分で撮影した写真を一覧表示するコンポーネントを作成し、
各画像を-img-タグで表示

エラー発生の原因

そこで以下のように'onClick'属性を付与し、その先で'this.setState'としてところ上記のエラーが発生

画像表示タグ
<img src=... onClick={this.handlePopup()} />
setStateするために呼び出した関数
handlePopup() {
    this.setState({
        -stateの指定-
    })
};

解決策

'onClick'属性に直接関数を指定するのではなく、アロー関数で指定

onClick={() => this.handlePopup()}

参考にさせていただいたサイト

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?