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.

stateが更新されない(レンダリングとstateを理解する)

Last updated at Posted at 2023-06-15

事象

私は、Reactを使ってフォームを作成していました。
stateでデータを保持していたのですが、登録ボタンを押下すると、DatePikerで選択したはずの値で更新されておらず、1つ前のデータを保持した状態でDBに保存されるという、いやな事が起きてしまいました。

上記現象が起きたコンポーネントの構造は、
ページコンポーネント(親)から、自作のDatePicker(inputbox付き)を呼んでいて自作のDatePicker(子)から、MUIのDateCalendarを呼んでいるという風になっております。

改善

// stateの宣言
const [value, setValue] = useState<string>('');

// 更新時
- setValue('更新したで。');
+ setValue((value) => {return '更新したで。'});

参考

参考にさせていただきました、ありがとうございました:bow_tone2:

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?