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 3 years have passed since last update.

useStateでstateを変更しているのにリアルタイムに再描画されない

Posted at

結論

一時変数にstateを入れて、配列やオブジェクトの場合は展開して新しい配列として扱いましょう。


let temp = { ...props.samples };

ダメなやり方

一時変数に入れてるから以下のようにすれば大丈夫でしょって思っても、これは参照渡しになっています。
同じオブジェクトをいじろうとしてもうまく行きません。新しい配列を作るために展開してあげる必要があります。

//props.samples={hoge:"hoge", fuga:"fuga"}
let temp = props.samples; 
setState(temp)
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?