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.

JSX で強制的にレンダリングしたい

Last updated at Posted at 2022-08-17

本当に強制的にレンダリングしたいのか?

強制的にレンダリングしたいということではなくて、期待した再レンダリングが実行されなくて困っているのではないでしょうか?

何かの値が更新されたので、この値に従って再レンダリングして欲しかったのに、うまくいかないってこと、よくあります。

React は state や props の変更が再レンダリングのトリガーになります。通常はこれで十分な場合が多いのですが、うまくいかないときは useState() フックを利用すると再レンダリングが可能になります。

useState() フックを利用する

更新のトリガーにしたいオブジェクトが更新されたとき、期待した再レンダリングが実行されない場合、このオブジェクトを useState() で保持すると、値の変化をトリガーして確実に再レンダリングされるようになります。

それでも本当に強制的に再レンダリングしたい場合

公式に「forceUpdate のようなものはありますか?」というページがありますので、この実装でできるはずです。

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?