【发布时间】:2021-08-24 12:41:22
【问题描述】:
我做了一个 instagram 克隆,我有一个新闻提要,您应该在其中看到用户发布的图片,但在我的情况下,图片没有显示。
我找不到任何错误不知道我做错了什么如果我拍照并上传它然后图片将保存在 Firebase 数据库中,并将显示在我的个人资料中,但不会显示在 Feed 中。
这是 Profile.js 文件:
import React, { useState, useEffect } from 'react';
import { StyleSheet, View, Text, Image, FlatList, Button } from 'react-native';
import { connect } from 'react-redux';
import firebase from 'firebase/app';
import 'firebase/firestore'
function Profile(props) {
const [userPosts, setUserPosts] = useState([]);
const [user, setUser] = useState(null);
const [following, setFollowing] = useState(false)
useEffect(() => {
const { currentUser, posts } = props;
console.log({ currentUser, posts });
if (props.route.params.uid === firebase.auth().currentUser.uid) {
setUser(firebase.auth().currentUser);
setUserPosts(posts);
}else{
firebase.firestore()
.collection("users")
.doc(props.route.params.uid)
.get()
.then((snapshot) =>{
if(snapshot.exists){
setUser(snapshot.data())
}else{
console.log('does not exist')
}
})
firebase.firestore()
.collection("posts")
.doc(props.route.params.uid)
.collection("userPosts")
.orderBy("creation", "asc")
.get()
.then((snapshot) =>{
let posts = snapshot.docs.map(doc => {
const data = doc.data();
const id = doc.id;
return{id, ...data}
})
setUserPosts(posts)
})
}
if(props.following.indexOf(props.route.params.uid) > -1){
setFollowing(true);
}else{
setFollowing(false)
}
},[props.route.params.uid, props.following]);
const onFollow = () =>{
firebase.firestore()
.collection("following")
.doc(firebase.auth().currentUser.uid)
.collection("userFollowing")
.doc(props.route.params.uid)
.set({})
}
const onUnFollow = () =>{
firebase.firestore()
.collection("following")
.doc(firebase.auth().currentUser.uid)
.collection("userFollowing")
.doc(props.route.params.uid)
.delete()
}
const onLogout = () =>{
firebase.auth().signOut();
}
if (user === null) {
return <View />;
}
return (
<View style={styles.container}>
<View style={styles.containerInfo}>
<Text> {user.name} </Text>
<Text> {user.email} </Text>
{props.route.params.uid !== firebase.auth().currentUser.uid ? (
<View>
{following ? (
<Button
title="Following"
onPress={() => onUnFollow()}
/>
) :
(
<Button
title="Follow"
onPress={() => onFollow()}
/>
)}
</View>
) : <Button
title="Logout"
onPress={() => onLogout()}
/>}
</View>
<View style={styles.containerGallery}>
<FlatList
numColumns={3}
horizontal={false}
data={userPosts}
renderItem={({ item }) => (
<View style={styles.containerImage}>
<Image style={styles.image} source={{ uri: item.downloadURL }} />
</View>
)}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
containerInfo: {
margin: 20,
},
containerGallery: {
flex: 1,
},
containerImage: {
flex: 1 / 3,
},
image: {
flex: 1,
aspectRatio: 1 / 1,
},
});
const mapStateToProps = (store) => ({
currentUser: store.userState.currentUser,
posts: store.userState.posts,
following: store.userState.following
});
export default connect(mapStateToProps, null)(Profile);
Feed.js 文件:
import React, { useState, useEffect } from 'react';
import { StyleSheet, View, Text, Image, FlatList, Button } from 'react-native';
import { connect } from 'react-redux';
import firebase from 'firebase/app';
import 'firebase/firestore'
function Feed(props) {
const [posts, setPosts] = useState([]);
useEffect(() => {
let posts = [];
if(props.usersLoaded == props.following?.length){
for(let i = 0; i < props.following?.length; i++){
const user = props.users.find(el => el.uid === props.following[i]);
if(user != undefined){
posts = [...posts, ...user.posts]
}
}
posts.sort(function(x,y) {
return x.creation - y.creation;
})
setPosts(posts)
}
},[props.usersLoaded]);
return (
<View style={styles.containerGallery}>
<FlatList
numColumns={1}
horizontal={false}
data={posts}
renderItem={({ item }) => (
<View style={styles.containerImage}>
<Text style={styles.container}>{item.user.name}</Text>
<Image style={styles.image} source={{ uri: item.downloadURL }} />
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
containerInfo: {
margin: 20,
},
containerGallery: {
flex: 1,
},
containerImage: {
flex: 1 / 3,
},
image: {
flex: 1,
aspectRatio: 1 / 1,
},
});
const mapStateToProps = (store) => ({
currentUser: store.userState.currentUser,
following: store.userState.following,
users: store.usersState.users,
usersLoaded: store.usersState.usersLoaded,
});
export default connect(mapStateToProps, null)(Feed);
【问题讨论】:
-
你确定图片 url 正在改变吗?
-
是的,我确定@Amruth
标签: reactjs react-native expo