Reactjs 从数据库加载后是否未显示图像?
我是个新来的土生土长的人。当打印到我的控制台Reactjs 从数据库加载后是否未显示图像?,reactjs,react-native,Reactjs,React Native,我是个新来的土生土长的人。当打印到我的控制台snapshot.val()时,我有一个用户的配置文件图片正试图显示在屏幕上。imageURL使用以下方式显示正确的url: var profImage = 'https://www.placeholdit.com'; var user = authFB.currentUser; if (user) { database.ref('users').child(user.uid).once('value') .then((snapshot)
snapshot.val()时,我有一个用户的配置文件图片正试图显示在屏幕上。imageURL
使用以下方式显示正确的url:
var profImage = 'https://www.placeholdit.com';
var user = authFB.currentUser;
if (user) {
database.ref('users').child(user.uid).once('value')
.then((snapshot) => profImage = snapshot.val().imageURL)
.catch(error => console.log(error))
} else {
console.log("No user")
}
我将该url分配给profImage
,并尝试将我的图像uri设置为该变量:
<Avatar
large
rounded
source={{ uri: profImage }}
onPress={() => alert(profImage)}
activeOpacity={0.7}
containerStyle={{ marginBottom: 12 }}
/>
请试试这个
<Avatar
large
rounded
source={{ uri: img }}
onPress={() => alert(profImage)}
activeOpacity={0.7}
containerStyle={{ marginBottom: 12 }}
/>
alert(profImage)}
activeOpacity={0.7}
containerStyle={{marginBottom:12}}
/>
取代
<Avatar
large
rounded
source={{ uri: profImage }}
onPress={() => alert(profImage)}
activeOpacity={0.7}
containerStyle={{ marginBottom: 12 }}
/>
alert(profImage)}
activeOpacity={0.7}
containerStyle={{marginBottom:12}}
/>
这是因为您在呈现方法本身内部调用了异步请求。
当您的呈现方法执行时,您请求imageUrl,这是一个异步调用,需要时间来解析和更新profImage变量的值。此时,渲染方法完成执行并设置占位符。
如果需要执行此操作,则应将此profImage保持在组件状态。因此,一旦状态更新,将再次调用渲染方法并使用新图像更新UI
试试这个
class Profile extends React.Component {
static navigationOptions = {
title: "Profile",
headerStyle: {
backgroundColor: '#fff',
borderBottomWidth: 0,
},
headerTitleStyle: {
color: 'black'
},
}
constructor() {
super();
this.state = {
image: null,
uploading: null,
}
}
componentWillRecieveProps(nextProps){
var profImage = 'https://www.placeholdit.com';
var user = authFB.currentUser;
if (user) {
database.ref('users').child(user.uid).once('value')
.then((snapshot) => this.setState({ image: snapshot.val().imageURL }))
.catch(error => console.log(error))
} else {
this.setState({ image: 'https://www.placeholdit.com' });
console.log("No user")
}
}
render() {
const { navigate } = this.props.navigation;
let { image } = this.state;
return (
<ScrollView>
<View style={styles.profileTopContainer}>
<Avatar
large
rounded
source={{ uri: image }}
onPress={() => alert(profImage)}
activeOpacity={0.7}
containerStyle={{ marginBottom: 12 }}
/>
<Text
style={styles.usernameText}
>
Jordan Lewallen
</Text>
<TouchableHighlight
onPress={this._pickImage}>
<Text
style={styles.userMinutes}
>
Choose a profile picture
</Text>
</TouchableHighlight>
{this._maybeRenderImage()}
{this._maybeRenderUploadingOverlay()}
{
(image)
? <Image source={{ uri: image }} style={{ width: 250, height: 250 }} />
: null
}
</View>
}
类配置文件扩展了React.Component{
静态导航选项={
标题:“简介”,
头型:{
背景颜色:“#fff”,
边框底部宽度:0,
},
头饰样式:{
颜色:“黑色”
},
}
构造函数(){
超级();
此.state={
图像:空,
上传:空,
}
}
组件将接收道具(下一步){
变量profImage=https://www.placeholdit.com';
var user=authFB.currentUser;
如果(用户){
database.ref('users').child(user.uid).once('value'))
.then((快照)=>this.setState({image:snapshot.val().imageURL}))
.catch(错误=>console.log(错误))
}否则{
this.setState({image:'https://www.placeholdit.com' });
console.log(“无用户”)
}
}
render(){
const{navigate}=this.props.navigation;
设{image}=this.state;
返回(
警报(profImage)}
activeOpacity={0.7}
containerStyle={{marginBottom:12}}
/>
乔丹·勒瓦伦
选择个人资料图片
{this.\u maybeRenderImage()}
{this.\u可能欠加载overlay()}
{
(图片)
?
:null
}
}
此逻辑完全有道理!但是,您提供的示例不起作用,我得到一个找不到变量:img
错误。我实际使用图像
组件状态变量名称进行实际图像上载(需要更改语义)但是为了清楚起见,你能让你的状态变量,我需要使用像stackOverflowImage
这样的东西来帮助区分吗?感谢到目前为止!这很奇怪,现在它让我对NSNull类型的JSON值不能转换为有效的URL
大惊小怪,所以似乎image
没有被设置为URL从数据库中提取?@csathi我想我们看到的是有意义的,因为它甚至在异步函数完成之前就抛出了错误。对吗?snapshot.val()。imageURL可能不会给出URL。我想您需要在这里设置正确的变量名。
class Profile extends React.Component {
static navigationOptions = {
title: "Profile",
headerStyle: {
backgroundColor: '#fff',
borderBottomWidth: 0,
},
headerTitleStyle: {
color: 'black'
},
}
constructor() {
super();
this.state = {
image: null,
uploading: null,
}
}
componentWillRecieveProps(nextProps){
var profImage = 'https://www.placeholdit.com';
var user = authFB.currentUser;
if (user) {
database.ref('users').child(user.uid).once('value')
.then((snapshot) => this.setState({ image: snapshot.val().imageURL }))
.catch(error => console.log(error))
} else {
this.setState({ image: 'https://www.placeholdit.com' });
console.log("No user")
}
}
render() {
const { navigate } = this.props.navigation;
let { image } = this.state;
return (
<ScrollView>
<View style={styles.profileTopContainer}>
<Avatar
large
rounded
source={{ uri: image }}
onPress={() => alert(profImage)}
activeOpacity={0.7}
containerStyle={{ marginBottom: 12 }}
/>
<Text
style={styles.usernameText}
>
Jordan Lewallen
</Text>
<TouchableHighlight
onPress={this._pickImage}>
<Text
style={styles.userMinutes}
>
Choose a profile picture
</Text>
</TouchableHighlight>
{this._maybeRenderImage()}
{this._maybeRenderUploadingOverlay()}
{
(image)
? <Image source={{ uri: image }} style={{ width: 250, height: 250 }} />
: null
}
</View>
}