英文:
Despite I get different values of images in base64 when fetching data, but the same image is output for all images when rendering
问题
以下是您提供的内容的翻译部分:
我有一个服务器,我在其中以base64格式存储用户图像。我尝试获取并呈现它,但我一直得到相同的图像。我理解问题很可能是React的特定工作,但我不明白问题究竟是什么。
这是我尝试过的内容:
我有一个渲染具有不同props“name”的Dialog组件的Viewer类。
class Viewer extends Component {
render() {
return (
<View>
<Dialog name={'0'} id={'0'}/>
<Dialog name={'54353'} id={'54353'}/>
<Dialog name={'54354'} id={'54354'}/>
</View>
);
}
}
在Dialog中,我有一个获取以base64格式获取数据的getImage方法,当componentDidMount时将其设置为“img”。
componentDidMount(){
this.img = this.getImage(this.name);
}
getImage(_id) {
//this.setState({ loading: true });
const fileBlob = new FileReader(); // BLOB
/*this.setState({
loading: true
});*/
fetch(domenName + '/server/getUserImageById.php?user_id=' + _id)
.then(response => response.blob())
.then(response => {
this.loading = false;
this.setState({loading: false});
response.size == 0 ? img = domenName + '/assets/img/noimage.png':
[fileBlob.readAsDataURL(response), fileBlob.onload = () => {
//console.log(this.loading);
img = fileBlob.result;
//console.log("id:" + _id);
}]
}).catch(error => {
this.loading = false;
//this.setState({loading: false});
//console.log('error')
})
return img;
}
尽管在获取数据时我获得不同值的base64图像,但在呈现时所有图像都输出相同的图像。
class Dialog extends Component {
render(){
console.log(this.loading);
if(this.loading){
return (
<View>
<Text>Loading...</Text>
<Text>我是用户 №{this.id}!</Text>
</View>
);
}
else{
return (
<View>
<Image
key={this.id}
source={{uri: this.img}}
style={{ width: 60, height: 60, borderRadius: 30 }}
/>
<Text>我是用户 №{this.id}!</Text>
</View>
);
}
}
}
示例链接:https://snack.expo.dev/@konst1966/ea6dad
英文:
I have a server where I store user images in base64 format. Im trying to fetch and render it, but I keep getting the same image. I understand that the problem is most likely in the particular work of react, but I don't understand what exactly the problem is.
Here's what I tried:
I have class Viewer that renders components Dialog with different props "name"
class Viewer extends Component {
render() {
return (
<View>
<Dialog name={'0'} id={'0'}/>
<Dialog name={'54353'} id={'54353'}/>
<Dialog name={'54354'} id={'54354'}/>
</View>
);
}
}
In Dialog I have method getImage that fetch data in base64 and when componentDidMount setting it to "img"
componentDidMount(){
this.img = this.getImage(this.name);
}
getImage(_id) {
//this.setState({ loading: true });
const fileBlob = new FileReader(); // BLOB
/*this.setState({
loading: true
});*/
fetch(domenName + '/server/getUserImageById.php?user_id=' + _id)
.then(response => response.blob())
.then(response => {
this.loading = false;
this.setState({loading: false});
response.size == 0 ? img = domenName + '/assets/img/noimage.png':
[fileBlob.readAsDataURL(response), fileBlob.onload = () => {
//console.log(this.loading);
img = fileBlob.result;
//console.log("id:" + _id);
}]
}).catch(error => {
this.loading = false;
//this.setState({loading: false});
//console.log('error')
})
return img;
}
Despite I get different values of images in base64 when fetching data, but the same image is output for all images when rendering
class Dialog extends Component {
render(){
console.log(this.loading);
if(this.loading){
return (
<View>
<Text>Loading...</Text>
<Text>Im user №{this.id}!</Text>
</View>
);
}
else{
return (
<View>
<Image
key = {this.id}
source={{uri: this.img}}
style={{ width: 60, height: 60, borderRadius: 30 }}
/>
<Text>Im user №{this.id}!</Text>
</View>
);
}
}
}
Link to sandbox with example:
https://snack.expo.dev/@konst1966/ea6dad
答案1
得分: 0
将fetch()
函数移到componentDidMount
解决了这个问题。
另外,在.then(response)=>{}
中添加以下代码:
this.setState({image:img})
英文:
Solved the problem by moving the fetch() function to componentDidMount.
Also adding to .then(response)=>{} this code:
this.setState({image:img})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论