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 1 year has passed since last update.

無限ループせずにCountをとる

Posted at

やり方

useRefを使用して、useEffect内で.currentをインクリメントする。
useRefの.currentはstateではないためインクリメント時にuseEffectは呼ばれない。
20848D37-7739-4005-B6AB-9CDC81BFE705.png

NG例

inputが変わるたびにuseEffect内でsetCount関数を呼んでcountをアップデートする。
この場合、countが変更されるたびにuseEffectが呼ばれるため、無限ループになってしまう。
D7B17489-B7D0-4FCA-9B45-85BADA904EB2.png

参考ビデオ:https://youtu.be/t2ypzz6gJm0

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?