首页 > 解决方案 > 如何将 firestoreConnect 指向 React-Redux-Firebase 中的嵌套集合?

问题描述

下面的行指向firestoreConnect我的收藏,标签为projects

 { collection: 'projects' }

当项目集合立即位于根目录下时,它可以工作,如下所示:

root/
  projects/

但是,如果项目集合被本身位于另一个集合中的文档引用,例如,像这样:

root/
  users/
    alice/
      projects/

我如何指向firestoreConnect集合projects

文件没有提及此事。

视频链接
import React, { Component } from 'react'
import ProjectList from '../projects/ProjectList'
import Notifications from './Notifications'
import { connect } from 'react-redux'
import { firestoreConnect } from 'react-redux-firebase'
import { compose } from 'redux'
import { Redirect } from 'react-router-dom'

class Dashboard extends Component {...}

const mapStateToProps = (state) => {
  // console.log(state);
  return {
    projects: state.firestore.ordered.projects,
    auth: state.firebase.auth,
  }
}

export default compose(
  connect(mapStateToProps),
  firestoreConnect([
    { collection: 'projects' }, // <-- Question is about this line
  ])
)(Dashboard)

编辑:不成功的尝试

从我可以从这里的评论中拼凑出来的答案似乎是:

firestoreConnect([
  { 
    collection : 'users',
    doc        : 'alice',
    collection : 'projects',
  }
])

但这种尝试失败了。

标签: reactjsfirebasereact-reduxgoogle-cloud-firestorereact-redux-firebase

解决方案


可以通过将多个集合/文档设置传递给subcollections参数来完成,如下所示:

firestoreConnect(() => [
  {
    collection: 'states',
    doc: 'CA',
    subcollections: [
      { collection: 'cities', doc: 'SF' },
      { collection: 'zips' }
    ]
  }
])

在 react-redux-firebase 文档的 firestoreConnect 部分中有说明(因为它是特定于反应的 HOC)。可以传递给查询的选项记录在 redux-firestore 的 README 中

如果你正在做嵌套的子集合,你可能会想要使用这些v1.*.*版本,因为子集合以 redux 状态存储在侧顶级集合中。请注意,新版本具有不同的 API,如 v1.0.0 路线图中所述

披露:我是 redux-firestore 的作者


推荐阅读