English 中文(简体)
Despite I get different values of images in base64 when fetching data, but the same image is output for all images when rendering
原标题:

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

Output

    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

问题回答

暂无回答




相关问题
Using QCView and iSight to capture image

I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...

Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Transform rectangular image into trapezoid

In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...

Rotate image through Y-axis on web page

What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

Convert from 32-BPP to 8-BPP Indexed (C#)

I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...

热门标签