2

I am beginning to connect my store to mobx. I wonder the difference between the use of observer(['store'],...) or the use of inject('store')(observer(...))

The main difference I see it that inject is not reactive. So what the intents of inject over observer exactly?

4

1 に答える 1

6

observer(['store'], Component)と の間に違いはありませんinject('store')(observer(Component))

observerデコレータは、最初の引数が配列かどうかをチェックします。配列の場合は、inject internalを呼び出します。

function observer(arg1, arg2) {
...
 if (Array.isArray(arg1)) {
   ...
   return inject.apply(null, arg1)(observer(arg2));
  }
...

injectただし、 と の間にさらにデコレータを追加できますobserver。その場合、違いがあります。中間のデコレーターは、注入された小道具にアクセスできます。を使用しない場合、外側のデコレータは明らかに、注入された小道具にアクセスできませんinject

injectMobX にはコンテキスト内のものを渡すメカニズムがすでにあるため、小道具を挿入するのに便利な方法です。コンポーネントをリアクティブコンポーネントに変換せずにプロパティをコンポーネントに注入したい場合は、別のライブラリ (react-tunnelたとえば) を使用する必要があります。

于 2016-07-26T14:03:06.500 に答える