javascript - React 中的图像 onError 处理

标签 javascript reactjs image youtube onerror

我正在尝试获取多个 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>
    )
  }
}

最佳答案

为此,我建议渲染 <img />根据你的状态 <FeatureVideo/>组件。

例如,您可以创建一个 Image对象,尝试加载 background图像,并由此可靠地确定图像是否加载失败。在图像加载成功或失败时,您将 setState()在你的 <FeaturedVideo/>具有适当 background 的组件该值将用于呈现实际的 <img/>元素:

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

希望对您有所帮助!

关于javascript - React 中的图像 onError 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999458/

相关文章:

javascript - 在反向字符串javascript中保持零值

javascript - 一个网站可以同时使用简单的 JavaScript 和 React 吗

image - 使用opencv仅去除图像中的黑点

c++ - 写入 .PGM 图像会导致形状困惑

c++ - 如何访问 MacBook 上的网络摄像头?

javascript - jQuery - 检查所选行是否是表格中最后可见的行

javascript - 防止 javascript 代码在 asp.net 中转义

javascript - 将 Polymer 3 路由更改为不同的 html 页面

reactjs - 在 Sider 中的 Ant Design Menu 中重置 openKeys 的问题

javascript - 如何在 pageInfo 中将总计数传递给客户端