【发布时间】:2021-09-21 23:32:43
【问题描述】:
我是 React Native 的新手,使用 Expo,我能够成功设置一个将图片上传到 Firebase 存储的应用,但现在我无法在应用(主屏幕)上显示这些图片。
如何将最新图像拉入/显示到 FlatList 或类似的可滚动组件中?我已经通过 StackOverflow 查看了以前的答案,但没有运气。 感谢您的帮助!
HomeS.js:
export default class HomeS extends React. Component {
renderPost = post => {
return (
<View style={styles.feedItem}>
<Image source={post.avatar} style={styles.avatar} />
<View style={{ flex: 1 }}>
<View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
<View>
<Text style={styles.name}>{post.name}</Text>
<Text style={styles.timestamp}>{moment(post.timestamp).fromNow()}</Text>
</View>
<Feather name="more-horizontal" size={24} color="#73788B" />
</View>
<Text style={styles.post}>{post.text}</Text>
<Image source={post.image} style={styles.postImage} resizeMode="cover" />
<View style={{ flexDirection: "row" }}>
<Feather name="heart" size={24} color="#73788B" style={{marginRight:16}} />
<Ionicons name="chatbox" size={24} color="#73788B" />
</View>
</View>
</View>
);
};
constructor(props){
super(props);
this.state=({
posts:[],
newtext:'',
loading:false,
});
this.ref = firebase.firestore().collection('posts').orderBy("timestamp", "desc");
}
componentDidMount() {
const {imageName} = this.state;
let imageRef = firebase.storage().ref('photos' + imageName);
imageRef
.getDownloadURL()
.then((url) => {
//from url you can fetched the uploaded image easily
this.setState({profileImageUrl: url});
})
.catch((e) => console.log('getting downloadURL of image error => ', e));
this.unsubscribe = this.ref.onSnapshot((querySnapshot => {
const example = [];
querySnapshot.forEach((doc, index)=>{
example.push({
name: doc.data().name, //Work
id: doc.data().id, //Work
text: doc.data().text, //Work
timestamp: doc.data().timestamp, //Work
imageRef: doc.data().imageRef // Not Working
});
});
this.setState({
posts:example,
loading: false,
});
}));
}
onPressPost = () => {
this.ref.add({
textname : this.props.text,localUri: this.state.image
}).then((data)=>{
console.log(`adding data = ${data}`);
this.setState({
newtext:'',
image:null,
loading:true
});
}).catch((error)=>{
console.log(`error to add doc = ${error}`);
this.setState({
newtext:'',
loading:true
});
});
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>Feed</Text>
</View>
<FlatList
style={styles.feed}
data={this.state.posts}
renderItem={({ item }) => this.renderPost(item)}
keyExtractor={item => item.id}
showsVerticalScrollIndicator={false}
></FlatList>
</View>
);
}
【问题讨论】:
-
有人吗?
-
一会儿我会回答的
标签: react-native