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.

「状態共有」と「状態管理ロジックの共通化」は違う

0
Posted at

タイトルの通りなのですが、意外と混同されている気がするので軽く説明します。

状態共有とは、例えば複数コンポーネント間で同じ状態を参照することです。

Reduxなどの状態管理ストアを利用します。VueだとVuexですね。

イメージとしてはシングルトンのインスタンスをみんなで共有している感覚です。

状態管理ロジックの共通化とは、例えば複数のコンポーネントがそれぞれに状態を持つのだけれども、状態Aから状態Bに変化するための変換ロジックのみを共通化して再利用することです。

ReactだとHigh Order Component(HOC)やReact hooksを利用します。VueだとComposition APIやMixin(辛い)ですね。

状態共有でもロジックそのものが共通的に利用できるのは確かですが、同じ状態を参照しているので別のコンポーネントに切り替えるときにinitHoge()という感じの初期化ロジックを入れないと状態(前のコンポーネントの”お古”を)を引き継いでしまうために意図しない挙動になって困惑したりします。

なので、今やりたいことが状態共有なのか状態管理ロジックの共通化なのかを切り分けて考える必要があります。

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?