【发布时间】:2016-11-26 10:26:19
【问题描述】:
我正在编写一个模块,它从 json 获取文章数据并在文章文本上显示一个大图像,正如他们所说的那样,一个英雄模块。
我已经获得数据并进行了设置,因此如果有图像,它将显示该图像,如果数据中没有图像,它将显示默认图像。 问题是此方法不能替换断开的链接以显示默认图像。
我仍然是反应和使用状态的新手......问题是,我应该使用状态来检查损坏的链接吗?我该怎么做?
这就是我在模块中将数据作为道具获取的方式:
const { newsItemData: {
headline = '',
bylines = [],
publishedDate: publishDate = '',
updatedDate: updatedDate = '',
link: newsLink = '',
contentClassification: category = '',
abstract: previewText = '',
abstractimage: { filename: newsImage = '' } = {},
surfaceable: { feature: { type: featureType = '' } = {} } = {},
} = {},
wideView,
showPill,
defaultImage } = this.props;
我以这种方式显示信息:
<div className={imageContainerClassName} style={customBackgroundStyles}>
{newsImage ? <img className="img-responsive" src={newsImage} alt={headline}/> : <img className="img-responsive" src={defaultImage} alt={headline}/>}
</div>
我应该怎么做才能同时检查损坏的图像? 我认为这是所需的所有相关数据,如果我应该显示其他任何内容,请告诉我。 谢谢!
【问题讨论】:
-
我写了一些可以解决这个问题的东西:github.com/mbrevda/react-img-multi
-
找到了这个视频教程 - youtu.be/90P1_xCaim4 它实际上帮助我为我的应用程序构建了一个完整的图像组件。我还发现了这个以及我的图像组件的很棒的预加载器 - youtu.be/GBHBjv6xfY4。通过将两者结合起来,您可以为用户提供出色的用户体验。
标签: javascript reactjs error-handling state