【问题标题】:Image onError Handling in ReactReact 中的图像 onError 处理
【发布时间】:2019-03-30 16:00:44
【问题描述】:

我正在尝试获取多个 YouTube 缩略图中的 maxresdefault。一些 YouTube 视频根本没有高分辨率缩略图照片,所以我想用 img 元素上的 onError 道具来捕捉它。出于某种原因,当我收到 404 img 错误时,我的功能没有触发。有任何想法吗?提前致谢。

class FeaturedVideo extends Component<Props> {
  addDefaultSrc = (e) => {
    e.target.src = this.props.background.replace("maxresdefault", "hqdefault")
  }

  renderVideo = (props) => (
    <div
      style={{
        width: "100%",
        height: "100%",
        backgroundSize: "contain",
      }}
      className="featured-community-video"
    >
      <img onError={this.addDefaultSrc} src={props.background} alt="" />
      <div className="featured-community-video-title">
        <h2 style={{ fontSize: "0.8em" }}>WATCH</h2>
        <h1 style={{ fontSize: props.titleFontSize }}>{props.title}</h1>
      </div>
    </div>
  )

  render() {
    return (
      <div
        key={this.props.postId}
        style={{
          width: this.props.width,
          height: "50%",
        }}
        className="featured-community-video-container"
      >
        <Link to={routeCodes.POST_DETAILS(this.props.postId)}>
          {this.renderVideo(this.props)}
        </Link>
      </div>
    )
  }
}

【问题讨论】:

    标签: javascript reactjs image youtube onerror


    【解决方案1】:

    为了实现这一点,我建议根据 &lt;FeatureVideo/&gt; 组件的状态渲染 &lt;img /&gt;

    例如,您可以创建一个Image 对象,尝试加载background 图像,然后可靠地确定图像是否加载失败。在图像加载成功或失败时,您将在&lt;FeaturedVideo/&gt; 组件上使用适当的background 值来setState(),而该值将用于呈现您实际的&lt;img/&gt; 元素:

    class FeaturedVideo extends Component<Props> {
    
      componentDidMount() {
    
        if(this.props.background) {
    
          // When component mounts, create an image object and attempt to load background
          fetch(this.props.background).then(response => {
             if(response.ok) {
               // On success, set the background state from background
               // prop
               this.setState({ background : this.props.background })
             } else {        
               // On failure to load, set the "default" background state
               this.setState({ background : this.props.background.replace("maxresdefault", "hqdefault") })
             }
          });
        }
      }
    
      // Update the function signature to be class method to make state access eaiser
      renderVideo(props) {
    
        return <div
          style={{
            width: "100%",
            height: "100%",
            backgroundSize: "contain",
          }}
          className="featured-community-video">
    
          {/* Update image src to come from state */}
    
          <img src={this.state.background} alt="" />
          <div className="featured-community-video-title">
            <h2 style={{ fontSize: "0.8em" }}>WATCH</h2>
            <h1 style={{ fontSize: props.titleFontSize }}>{props.title}</h1>
          </div>
        </div>
      }
    
      render() {
        return (
          <div
            key={this.props.postId}
            style={{
              width: this.props.width,
              height: "50%",
            }}
            className="featured-community-video-container"
          >
            <Link to={routeCodes.POST_DETAILS(this.props.postId)}>
              {this.renderVideo(this.props)}
            </Link>
          </div>
        )
      }
    }
    

    希望有帮助!

    【讨论】:

    • 由于某种原因,错误仍未触发。我在控制台中收到 404 错误,但 onError 方法没有触发。
    • 奇怪 - 只是为了检查一下,this.props.background; 属性的值是否与无法在开发控制台中加载的资源 url 匹配?
    • 是的,这是给我带来麻烦的特定网址https://i.ytimg.com/vi/pzRDaVxNCj8/maxresdefault.jpg。如您所见,https://i.ytimg.com/vi/pzRDaVxNCj8/hqdefault.jpg 工作得很好。除了 onError 方法之外,也许还有另一种方法?
    • 我认为它返回 404 以及默认的空白图像这一事实,DOM 不认为这是一个错误。
    • 那是正确的 - 刚刚更新了答案,这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2015-02-28
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    相关资源
    最近更新 更多