【问题标题】:react-komposer, composer functions that depend on prop provided by other composer functionsreact-komposer,composer函数,依赖于其他composer函数提供的prop
【发布时间】:2016-07-30 20:58:22
【问题描述】:

您好,我正在尝试编写依赖于其他作曲家函数提供的道具的作曲家函数。

这个提供道具叫a

import {useDeps, composeWithTracker, composeAll} from 'mantra-core';
import Component from '../components/component';

export const composerA = ({context, _id}, onData) => {
  const {Meteor,Collections} = context();
  const query = {_id};
  if (Meteor.subscribe('a', query).ready()) {
    const a = Collections.A.findOne(query);
    onData(null,{a});
  }
};

这个取决于名为a的道具。

// depends on `prop` named `a` which is provided  by `composerA`
export const composerB = ({context, a}, onData) => {
  const {Meteor,Collections} = contex();
  if(a){
    const query = {a_id : a._id};
    if (Meteor.subscribe('b', query).ready()) {
      const bs = Collections.B.find(query);
      onData(null,{bs});
    }
  }
};

当没有依赖时,它工作正常。

export const ThisWorks = compoaseAll(
  composeWithTracker(composerA),
  // some other composer function without dependency 
  composeWithTracker(composerWithoutDependency)
)(Component);

但是当我使用依赖于 A 的 composerB 组合它时,它会永远加载/等待。

export const ThisLoadsForever = composeAll(
  composeWithTracker(composerA),
  composeWithTracker(composerB)
)(Component);

我也试过这样做

export const temp = composeAll(
  composeWithTracker(composerA)
)(Component);
export const StillLoadsForever = composeAll(
  composeWithTracker(composerB)
)(temp);

我怀疑道具a 永远不会对composerB 可用或不为空,因此永远等待/加载。 我该如何解决?还是有其他方法可以组成具有依赖性的作曲家?

PS:我正在使用 mantra-js。

【问题讨论】:

    标签: meteor reactjs ecmascript-6


    【解决方案1】:

    composeAll 从右到左组成函数。如果你在 componentB 中依赖来自 componentA 的 prop,你需要将 componentB 放在 componentA 之上:

    export const ThisLoadsForever = composeAll( composeWithTracker(composerB), composeWithTracker(composerA) )(Component);

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2015-12-03
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      相关资源
      最近更新 更多