Reactjs 加载时图像渲染不正确

Reactjs 加载时图像渲染不正确,reactjs,Reactjs,我正在尝试将图像列表插入我的网页。我能够成功地做到这一点,但在页面上显示时遇到问题。我正在使用来显示图像。在这个包中,最初需要设置缩略图的高度和宽度。这给我带来了一个问题,因为每个图像的高度和宽度都不同。我想要的是使用Javascript来确定每个图像的高度和宽度,并动态地将它们设置为关联的图像 我对React不太熟悉,所以我尝试改变我声明图像大小的位置。我在componentDidMount、constructor等中设置了图像大小,但没有任何效果 constructor(props) {

我正在尝试将图像列表插入我的网页。我能够成功地做到这一点,但在页面上显示时遇到问题。我正在使用来显示图像。在这个包中,最初需要设置缩略图的高度和宽度。这给我带来了一个问题,因为每个图像的高度和宽度都不同。我想要的是使用Javascript来确定每个图像的高度和宽度,并动态地将它们设置为关联的图像

我对React不太熟悉,所以我尝试改变我声明图像大小的位置。我在componentDidMount、constructor等中设置了图像大小,但没有任何效果

constructor(props) {
    super(props);

    this.state = { stills: STILLS, val: 4 };

    this.setImgSize = this.setImgSize.bind(this);

    this.state.stills.map( (image, index) => {
      this.setImgSize(image, index);
    })
  }


setImgSize(image, index) {

    var newImg = new Image();

    newImg.onload = (function() {
      console.log("Onload");

      this.setState(state => {
        const stills = state.stills.map((still, id) => {
          if (id === index) {
            still.thumbnailHeight = newImg.height;
            still.thumbnailWidth = newImg.width;
          }
        })
      });
    }).bind(this);

    newImg.src = image.src; // this must be done AFTER setting onload
  }

图像最初以我设置的大小显示,但在离开页面并导航回页面后,图像大小正确。

React构造函数不是执行异步操作的最佳位置

“onload”方法是异步的,所以需要等待它返回结果。您可以通过使用Promise-and在componentDidMount生命周期方法中移动整个功能来实现这一结果

我修改了react grid gallery中“quick and dirty”示例中的代码,以读取图像缩略图的宽度和高度属性

setImgSize = (  image )=> {
    const promise = new Promise( function(resolve, reject) {

     let img = new Image();
     img.src = image.thumbnail;
     img.onload =  () => {
         // Should see width and height of images
         console.log( 'imge src', img.src, 'width ' , img.width, 'height', img.height);
         image.thumbnailHeight = img.height;
         image.thumbnailWidth = img.width;
         resolve('ok');               
          }

   });
   return promise;
}



componentDidMount() {
      Promise.all(this.IMAGES.map(this.setImgSize)).then(  (result) => { 
        //console.log('before state', result);
        this.setState({loading: false});
        console.log('after loop');
         }, function (error) {
               console.log('error ', error);
       });

}
以下是codesandbox中的完整代码:
React构造函数不是执行异步操作的最佳位置

“onload”方法是异步的,所以需要等待它返回结果。您可以通过使用Promise-and在componentDidMount生命周期方法中移动整个功能来实现这一结果

我修改了react grid gallery中“quick and dirty”示例中的代码,以读取图像缩略图的宽度和高度属性

setImgSize = (  image )=> {
    const promise = new Promise( function(resolve, reject) {

     let img = new Image();
     img.src = image.thumbnail;
     img.onload =  () => {
         // Should see width and height of images
         console.log( 'imge src', img.src, 'width ' , img.width, 'height', img.height);
         image.thumbnailHeight = img.height;
         image.thumbnailWidth = img.width;
         resolve('ok');               
          }

   });
   return promise;
}



componentDidMount() {
      Promise.all(this.IMAGES.map(this.setImgSize)).then(  (result) => { 
        //console.log('before state', result);
        this.setState({loading: false});
        console.log('after loop');
         }, function (error) {
               console.log('error ', error);
       });

}
以下是codesandbox中的完整代码: