【问题标题】:rxfire subscription not firing in componentdidmountrxfire 订阅未在 componentdidmount 中触发
【发布时间】:2021-07-24 01:23:38
【问题描述】:

我正在使用 rxfire 将来自 firestore 的数据合并到 componentdidmount 中:

store$.pipe(
      withLatestFrom(items$),  
      map(([store, items]) => ({ items, ...store })),    
    ).pipe(   
      withLatestFrom(categories$),       
      map(([store, categories]) => ({ categories, ...store })),
    ).subscribe(store => {      
      this.setState({ store: store, isLoading: false });
    })   

此代码的问题是没有触发并且屏幕卡在加载组件上。如果我第二次加载屏幕它就可以了。难道我做错了什么?我该如何解决它

【问题讨论】:

    标签: javascript firebase react-native rxjs rxfire


    【解决方案1】:

    如果在您重新加载之前它无法工作,则可能存在基于 items$categories$ 何时发出自己的值的时间问题。

    使用 RxJS 的 combineLatest() 功能可以更安全地组合这些 observables。它接受一个 observable 数组,并且在从 每个 observable 接收到一个值之前不会发出。

    combineLatest([store$, items$, categories$]).pipe(
      map(([store, items, categories])=>({...store, ...items, ...categories})),
      tap(store=>this.setState({ store, isLoading: false })
    ).subscribe();
    

    请记住combineLatest(),如果数组中的任何可观察对象不发出值,那么combineLatest() 将永远不会发出值。

    【讨论】:

    • 谢谢,它成功了。我只是这样改了:``` map(([store, products, categories])=>({products, categories, ...store})), ```否则,如果collection不发出,你是什么意思?:意思是如果类别或产品是空的,我们就没有价值?
    • 如果类别或产品为空,则它会发出一个空数组。我说的是 observable 是否发出零值。
    • 另外,如果这对您有帮助,请将此答案标记为解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多