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

React(importとexport) とらゼミ07 && (React Hooks) とらゼミ08 && (useEffectメソッド) とらゼミ09 メモ

0
Last updated at Posted at 2021-05-22

※exportとimportを詳しく知りたい時はハンズオンjavascriptを見た方が良い。

モジュール化とは

  • 基本的には1ファイルに1モジュール。
  • 任意の場所で使うことができる。
  • 分割統治できる。
  • 再利用できる。

名前なし(default)エクスポート。

  • ES6で推奨されているエクスポート。
  • クラスをエクスポートできる。

React Hooks

フック(Hook)とは

  • クラスの機能(stateやライフサイクル)をFunctional componentでも使える。
  • あくまで選択肢の一つ。

useState()

  • ステートフック。
  • this.stateとthis.setStateの代替。
  • 複数のstateを使う時はstate毎に宣言。

使い方

2cb82c93-910f-bec3-0fa0-ebb9f68d6365.png

useEffect()

  • ライフサイクルメソッドの代替できる。
  • functional componentでライフサイクルが使える。

書き方1

  • useEffect内でCallBack関数を書く。
  • CallBackはレンダー毎に呼ばれる。
  • returnするCallBackはunmount時に呼ばれる(クリーンアップ関数)。

書き方2

  • マウント時のみ実行。
  • useEffectは第二引数の配列内の値を前回レンダーと今回レンダーで比較、変更があればCallBack関数を実行。

書き方3

  • マウント、アンマウント時のみ実行。
  • 1,2の複合。

書き方4

  • 特定のレンダー時実行。
  • buttonなどで第二引数の値が変わった時に実行。
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?