LoginSignup
7
7

More than 1 year has passed since last update.

React Hooksの強制再レンダリング方法

Last updated at Posted at 2020-10-15

#React Hooksでの強制サイレンダリング方法
クラスコンポーネントでは、

this.forceUpdate()

が使えましたが、関数コンポーネントではそうは行きませんよね。
「形が不細工でもレンダーしたい!」という方にお勧めなのがこちらの方法です。

//boolean型のstateを作成
const [update,setUpdata]=useState<boolean>(false)
//レンダリングしたい場所でこれを差し込むだけ
setUpdata(update?false:true)

setUpdata(update?false:true)の役割は、updateステートがtrueだった場合"false"、falseだった場合"true"にするだけです。
ぜひ使っていきましょう。

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