【问题标题】:How to loop over images in my app如何在我的应用程序中循环图像
【发布时间】:2018-05-16 05:40:49
【问题描述】:

我正在尝试设置图像。当我设置第一张图像时它工作正常,但是当我尝试上传第二张图像时,它会创建第二个图像框,但不会在其上显示任何内容,而是将其显示在第一个图像框的顶部。我希望它们彼此分开。

我尝试通过在showImage函数中返回imgUrl(返回imgUrl)直接使用blob,但它也没有在img src中设置blob。 收到图片(URL)后,如何循环并在每个框中分别设置所有图像?

class Card extends Component {
  constructor({props, pic, token}) {
    super(props, pic, token);
    this.state = { 
      pic: pic
    };

readResponseAsBlob(response) {
  return response.blob();
}

validateResponse(response) {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  return response;
}

logError(error) {
  console.log('Looks like there was a problem: \n', error);
}

showImage(responseAsBlob) {
  var container = document.getElementById('images');
  var imgElem = container.getElementsByTagName('img')[0];
  var imgUrl = URL.createObjectURL(responseAsBlob);
  imgElem.src = imgUrl;
  console.log(imgUrl);
}


  urlFetch(data) {
   fetch(data, { 
   headers: new Headers({
     'authorization': `Bearer ${this.props.token}`, 
     'Content-Type': 'application/json'
    })
  })
    .then(this.validateResponse)
    .then(this.readResponseAsBlob)
    .then(this.showImage)
    .catch(this.logError);
  }


  render() {
    const { pic } = this.state;

return (
             <a style={{width: 200, height: 250}} className='tc dib br3 ma2 pa2 grow bw2 shadow-5'>
              <div id='images'>
               <img style={{width: 175, height: 175}} className='tc myimage br3' alt='none' src={ this.urlFetch(pic) }/>
              </div>
              </a>
   );
  }
}

【问题讨论】:

  • 对不起,我可能是盲人,但我没有看到任何用于生成图像框的循环。是在另一个班级吗?如果是的话,你能展示一下吗?
  • @LeoOdishvili 图像框是在从另一个组件接收到包含 URL 的“图片”时创建的。

标签: javascript html reactjs


【解决方案1】:

问题可能出在src={ this.urlFetch(pic) } 函数urlFetch 正在返回一个promise,而不是实际的图像路径或数据。 在componentDidMount 钩子中调用此函数,并将您从后端收到的图像设置为状态,并将该状态属性用作图像的src

我可以看到 urlFetch 函数手动修改 DOM 以设置图像 src,这有点令人困惑,因为您调用的函数正在隐式地完成它的工作,并且在 react 中修改 DOM 根本不是一个好习惯,而且var imgElem = container.getElementsByTagName('img')[0]; 只会修改第一个图像,因为您在容器中获得了第一个 img

我还建议在各自的组件中渲染每个图像,声明一个 state 属性,该属性将保存图像的src,获取服务器的图像,然后更新状态。

class Card extends Component {

  constructor({props, token}) {
    super(props, token);
    this.state = { 
      src: ''
    };
  }

 readResponseAsBlob = (response) => {
  return response.blob();
 }

showImage = (responseAsBlob) => {
this.setState({ src: URL.createObjectURL(responseAsBlob) });
 }

  componentDidMount() {
    this.urlFetch(this.props.pic)
  }

  logError(error) {
    console.log('Looks like there was a problem: \n', error);
  }

  urlFetch(data) {
    fetch(data, { 
    headers: new Headers({
       'authorization': `Bearer ${this.props.token}`, 
       'Content-Type': 'application/json'
      })
    })
    .then(this.validateResponse)
    .then(this.readResponseAsBlob)
    .then(this.showImage)
    .catch(this.logError);
  }


  render() {
    const { src } = this.state;

    return (
      <a style={{width: 200, height: 250}} className='tc dib br3 ma2 pa2 grow bw2 shadow-5'>
        <div id='images'>
          <img style={{width: 175, height: 175: display: src ? 'block': 'none'}} className='tc myimage br3' alt='none' src={ src }/>
        </div>
      </a>
    );
  }
}

【讨论】:

  • 我已经用示例@TedKhi 更新了答案,同样,当您将某些内容传递给组件时,它们只会在道具中接收它们,而不是作为构造函数的单独参数
  • TypeError: 无法读取 readResponseAsBlob 处未定义的属性“setState”
  • 从 promise 中移除绑定并在 readResponseAsBlob 中使用箭头函数有效。 readResponseAsBlob = (response) => { this.setState({src: response.blob()});控制台.log(this.state.src) }
  • 但是有一个问题。它不会使用 blob URL 更新 src。 src 仅包含大小和 mime 类型值。您可以在控制台日志中看到它。所以它不显示任何图像。
  • 我实际上无法在本地运行,您可以粘贴有问题的响应吗?
猜你喜欢
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多