2
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】バッチ更新で困る場合はupdater関数

2
Posted at

はじめに

  • Reactの基本的な動きの理解のために

問題

  • setHoge(hoge + 1) を2回実行してもReactのstateはバッチ更新されるため、今のhogeの値+1が2回実行されるだけで、値は1しか増えない
  • リアルなユースケースは思いつかないが、複雑化しているコードで使わざるを得ない場面はあるかもしれない

解決

  • Reactのstateは基本バッチ更新されるので、setHoge((好きな名前) => 好きな名前+1) を 2回書くupdater関数で更新ができる

終わりに

2
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
2
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?