将参数传递给 Firestore 集合
Pass params to firestore collections
我想知道是否可以将 params
像 busstop
传递给 this.ref = firebase.firestore().collection(params)
以在从一个页面导航到另一个页面时访问不同的集合。如果没有,我该如何实现?
第 1 页 - 用户可以选择按什么来从 firebase firestore 检索数据。
Page1.js
onPress () = > this.props.navigation.navigate('page2',{locations: 'busstops' })
onPress () = > this.props.navigation.navigate('page2',{locations: 'taxis' })
第2页
import {View,Text} from 'react-native';
const collectionid = this.props.navigation.state.params.location
const location = collectionid ? params.location : null;
export default class HomeDetail extends Component {
constructor(props) {
super(props);
this.ref = firebase.firestore().collection(location);
this.state = {
},
};
}
如果我没有正确理解你的问题,那么这是一个 react-navigation
问题,而不是 firestore 问题。请检查此 link:
来自文档:
In the past, you may have encountered the frightful scenario of
accessing a param when params is undefined. Instead of accessing the
param directly, you can call getParam instead.
他们将其更改为:
const { location } = this.props.navigation.state.params.location;
至
const location = this.props.navigation.getParam(paramName, defaultValue);
所以尝试在您的代码中使用它:
const collectionid = this.props.navigation.getParam('location');
我想知道是否可以将 params
像 busstop
传递给 this.ref = firebase.firestore().collection(params)
以在从一个页面导航到另一个页面时访问不同的集合。如果没有,我该如何实现?
第 1 页 - 用户可以选择按什么来从 firebase firestore 检索数据。
Page1.js
onPress () = > this.props.navigation.navigate('page2',{locations: 'busstops' })
onPress () = > this.props.navigation.navigate('page2',{locations: 'taxis' })
第2页
import {View,Text} from 'react-native';
const collectionid = this.props.navigation.state.params.location
const location = collectionid ? params.location : null;
export default class HomeDetail extends Component {
constructor(props) {
super(props);
this.ref = firebase.firestore().collection(location);
this.state = {
},
};
}
如果我没有正确理解你的问题,那么这是一个 react-navigation
问题,而不是 firestore 问题。请检查此 link:
来自文档:
In the past, you may have encountered the frightful scenario of accessing a param when params is undefined. Instead of accessing the param directly, you can call getParam instead.
他们将其更改为:
const { location } = this.props.navigation.state.params.location;
至
const location = this.props.navigation.getParam(paramName, defaultValue);
所以尝试在您的代码中使用它:
const collectionid = this.props.navigation.getParam('location');