使用react-redux-firebase将组件连接到redux时选择firestore子集合 [英] Choose firestore subcollection when connecting component to redux with react-redux-firebase
问题描述
我正在将 react-redux-firebase
的 fireStoreConnect()
中间件与我的本机移动应用程序中的屏幕.在将组件连接到redux存储时,我想指定我连接的firestore子集合,这取决于正在浏览该应用程序的用户.
I am using react-redux-firebase
's fireStoreConnect()
middleware with
a screen in my react-native mobile app. At the time of connecting the component to the redux store, I want to specify the firestore sub-collection I connect to, which depends on the user that is navigating the app.
如何在 firestoreConnect
中指定集合?用户ID位于redux存储中.
How should I specify the collection in firestoreConnect
? The user id is in the redux store.
MWE:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { compose } from 'redux';
import { connect } from 'react-redux'
import { firestoreConnect } from 'react-redux-firebase';
class PhotosScreen extends Component {
render() {
return (
<View>
<Text> i plan the use this.props.images here </Text>
</View>
);
}
}
const mapStateToProps = (state) => {
// reference the subcollection of the user
const images = state.firestore.data.images;
return {
images: images,
}
}
export default compose(
firestoreConnect([
{
collection: 'users',
doc: "HOW DO I GET THE USERS ID HERE? IT IS IN REDUX STORE",
subcollections: [{ collection: 'images' }]
}
]),
connect(mapStateToProps),
)(PhotosScreen)
推荐答案
在1.x版
const enhance = compose(
connect(
(state) => ({
someKey: state.someData
})
),
firebaseConnect(
(props, firebaseInstance) => [
{ path: `${props.someKey}/someData` }
]
)
)
在2.x版
firebaseConnect(
(props, store) => [
{ path: `${store.getState().someKey}/someData` }
]
)
请注意 firebaseConnect
中的第二个参数如何从 firebaseInstance
更改为 store
从v1更改为v2.
Note how the 2nd argument in firebaseConnect
changes from firebaseInstance
to store
from v1 to v2.
这应该可以为您提供所需的东西.
This should get you what you need.
这篇关于使用react-redux-firebase将组件连接到redux时选择firestore子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!