Despite I get different values of images in base64 when fetching data, but the same image is output for all images when rendering

huangapple go评论63阅读模式
英文:

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 (
          &lt;View&gt;
            &lt;Dialog name={&#39;0&#39;} id={&#39;0&#39;}/&gt;
            &lt;Dialog name={&#39;54353&#39;} id={&#39;54353&#39;}/&gt;
            &lt;Dialog name={&#39;54354&#39;} id={&#39;54354&#39;}/&gt;
          &lt;/View&gt;
        );
      }
}

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 + &#39;/server/getUserImageById.php?user_id=&#39; + _id)
          .then(response =&gt; response.blob())
          .then(response =&gt; {
              this.loading = false;
              this.setState({loading: false});
              response.size == 0 ? img = domenName + &#39;/assets/img/noimage.png&#39;:
                [fileBlob.readAsDataURL(response), fileBlob.onload = () =&gt; {
                  //console.log(this.loading);
                  img = fileBlob.result;
                  //console.log(&quot;id:&quot; + _id);
    
                }]
          }).catch(error =&gt; {
            this.loading = false;
            //this.setState({loading: false});
            //console.log(&#39;error&#39;) 
          })
        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

Output

    class Dialog extends Component {
      render(){
        console.log(this.loading);
    
        if(this.loading){
          return (
            &lt;View&gt;
              &lt;Text&gt;Loading...&lt;/Text&gt; 
              &lt;Text&gt;Im user {this.id}!&lt;/Text&gt; 
            &lt;/View&gt;    
          );
        }
        else{
          return (
            &lt;View&gt;
              &lt;Image 
                key = {this.id}
                source={{uri: this.img}} 
                style={{ width: 60, height: 60, borderRadius: 30 }} 
              /&gt;
              &lt;Text&gt;Im user {this.id}!&lt;/Text&gt; 
            &lt;/View&gt;    
          );
        }
      }
    }

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})

huangapple
  • 本文由 发表于 2023年5月18日 13:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定