I have 2 collections in Firestore to do FeedScreen like this
collection users (when user register App)
users = [
avatar: '',
email: '',
name: ''
]
collection Post (when user create a post)
posts = [
image: '',
text: '',
timestamp: '',
uid: ''
]
I do useState with collection posts like this
const [loading, setLoading] = useState(true);
const [post, setPost] = useState([]); // Initial empty array of users
useEffect(() => {
const subscriber = firebase
.firestore()
.collection("posts")
.orderBy("timestamp", "desc")
.onSnapshot((querySnapshot) => {
const post = [];
querySnapshot.forEach((doc) => {
post.push({
key: doc.id,
name: doc.data().name, //want name from collection users
text: doc.data().text,
timestamp: doc.data().timestamp,
avatar: { uri: doc.data().avatar }, //want avatar from collection users
image: { uri: doc.data().image },
});
});
setPost(post);
setLoading(false);
});
// Unsubscribe from events when no longer in use
return () => subscriber();
}, []);
if (loading) {
return <ActivityIndicator />;
}
And the Flatlist
<FlatList
style={styles.feed}
data={post}
renderItem={({ item }) => this.renderPost(item)}
keyExtractor={(item) => item.key}
showsVerticalScrollIndicator={false}
/>;
I try to useState collection('users') and combine it with map but it gives error
I would like to display feed to flatlist with all of the data in the post and fetch avatar and name from users collection.