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?

React勉強中に「Too many re-renders. React limits the number of renders to prevent an infinite loop.」というエラーが出た

0
Posted at

はじめに

stateの勉強中にエラーが起きた。

問題

num%3 === 0 ? setIsShow(true) : setIsShow(false)

Reactで上のコードを書いたときに以下のようなエラーが出る。

Too many re-renders. React limits the number of renders to prevent an infinite loop.

解決方法

num%3 === 0 ? isShow || setIsShow(true) : isShow && setIsShow(false)

無限にレンダリングが起きないように、trueのときはtrueにsetしない。falseのときはfalseにsetしない。

おわりに

前よりレンダリングを意識できるようになった。

0
0
1

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?