gpt4 book ai didi

javascript - Firebase + React Native - 获取每个文档 ID

转载 作者:行者123 更新时间:2023-12-01 01:15:02 25 4
gpt4 key购买 nike

多年来,我一直坚持这个问题,试图弄清楚当我按下每个呈现的 FlatList 项目时,如何通过控制台分别记录每个 Firebase Cloudstore 文档 ID。

我可以使用 onPress={() =>{console.log(this.state.posts[0].key)}} 等获取某个键/id。但我不这样做知道如何分别捕获每一个。本质上我只想要我按下的 touchableOpacity 的文档 ID。不只是[0]

下面是应用程序布局的屏幕截图,以便您可以了解并获取代码示例

PostsLayout.js

export default class PostsLayout extends React.Component {

render() {
const {summary, stringTime, user} = this.props;

return (

<TouchableOpacity
style={styles.container}
onPress={this.props.onPress}
>
<PostsUser user={user}/>
<PostsSummary summary={summary}/>
<PostsDate time={stringTime}/>
</TouchableOpacity>
)
}
}

FlatListLayout.js

    export default class FlatListLayout extends React.Component { 

render() {
return (
<ScrollView >
<FlatList
data={this.props.data}
renderItem={({item}) => <PostsLayout {...item} onPress={this.props.onPress}/>}
/>
</ScrollView>
)
}
}

ScreenLayout.js

export default class ScreenLayout extends React.Component { 

state = {
posts: []
}

db = firebase.firestore()
path = this.db.collection('usersData').doc(firebase.auth().currentUser.uid).collection("posts")


onCollectionUpdate = (querySnapshot) => {
const posts = [];
querySnapshot.forEach((doc) => {
const {summary, time, stringTime, user, userId} = doc.data();

posts.push({
key: doc.id, doc, summary,
time, stringTime, user, userId
});
});

this.setState({
posts
});

}

componentDidMount() {
const {currentUser} = firebase.auth();
this.setState({currentUser})

this.unsubscribe = this.path.onSnapshot(this.onCollectionUpdate)
}

componentWillUnmount() {
this.unsubscribe();
}

render() {
return (
<FlatListLayout
data={this.state.posts}
onPress={() => {console.log(this.state.posts[0].key)}}
/>
)
}
}

Here is an example of my app screen. Each grey box is a touchableOpacity and when I click it onto it I want to console log the firebase cloudstore document ID

感谢您阅读本文并请提供帮助:)

最佳答案

因此,最简单的修复方法是从子级别中的原始新闻事件发送一个函数参数。例如,PostsLayout有主要的onPress,因此在这个调用中只需发送回您需要的任何数据,每个组件都会有与该组件相关的特定数据。因为每个 child 的 react 都是独一无二的。PostsLayout.js

export default class PostsLayout extends React.Component {

handleOnPress = () => {
const { onPress, index } = this.props;
if( typeof onPress === 'function') {
onPress(this.props, index); // here pass anything you want in the parent level, like even userm stringtime etc
}
}

render() {
const {summary, stringTime, user} = this.props;

return (

<TouchableOpacity
style={styles.container}
onPress={this.handleOnPress}
>
<PostsUser user={user}/>
<PostsSummary summary={summary}/>
<PostsDate time={stringTime}/>
</TouchableOpacity>
)
}
}

FlatListLayout.js

    export default class FlatListLayout extends React.Component { 

render() {
return (
<ScrollView >
<FlatList
data={this.props.data}
renderItem={({item, index }) => <PostsLayout {...item} index={index} onPress={this.props.onPress}/>}
/>
</ScrollView>
)
}
}

ScreenLayout.js

export default class ScreenLayout extends React.Component { 

state = {
posts: []
}

db = firebase.firestore()
path = this.db.collection('usersData').doc(firebase.auth().currentUser.uid).collection("posts")


onCollectionUpdate = (querySnapshot) => {
const posts = [];
querySnapshot.forEach((doc) => {
const {summary, time, stringTime, user, userId} = doc.data();

posts.push({
key: doc.id, doc, summary,
time, stringTime, user, userId
});
});

this.setState({
posts
});

}

componentDidMount() {
const {currentUser} = firebase.auth();
this.setState({currentUser})

this.unsubscribe = this.path.onSnapshot(this.onCollectionUpdate)
}

componentWillUnmount() {
this.unsubscribe();
}

render() {
return (
<FlatListLayout
data={this.state.posts}
onPress={(data, index) => {console.log(data); console.log(this.state.posts[index].key)}}
/>
)
}
}

如果这没有任何意义,请告诉我:)

关于javascript - Firebase + React Native - 获取每个文档 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com