【发布时间】:2017-09-04 16:14:09
【问题描述】:
我正在尝试设计一个组件,用户可以在其中单击一个按钮,该按钮将触发一个 giphy API 调用,最终将呈现一系列 gif 图像。
到目前为止,除了实际的图像渲染之外,我能够成功完成所有工作。到目前为止,这是我的代码:
retrieveURLs() {
*bunch of stuff to make API call and return an array of URLs related
to the category of the button the user pushes*
}
renderGifs() {
this.retrieveURLs().then(function(results) {
console.log(results); //logs array of URLs
return results.map(function(url, index) {
console.log(url); //logs each url
return (<img key={index} src={url} alt="" />)
}, this);
});
}
render() {
return(
<div id="gif-div">
{this.renderGifs()}
</div>
)
}
尽管每个 console.log() 事件都表明 URL 至少被正确传递,但没有任何东西被渲染。
我为父组件做了类似的事情,以从如下所示的类别数组中呈现按钮:
btnArray = [*bunch of categories here*];
renderButtons() {
return btnArray.map(function(item, i) {
let btnID = btnArray[i].replace(/\s+/g, "+");
return <button type='button' className="btn btn-info" onClick={this.handleCategorySelect} key={i} id={btnID} value={btnID}>{btnArray[i]}</button>
}, this)
}
按钮渲染正确,但我的图像不正确。 renderbuttons 和 rendergifs 方法都不会改变状态。老实说,我看不出两者之间有什么有意义的区别,所以我想得到一些帮助,弄清楚为什么一个有效而另一个无效。
【问题讨论】:
-
您确定这些
img标签没有呈现在页面中吗?也许网址为空或错误... -
是的。应该嵌套图像的 div 元素内的页面没有呈现任何内容。
标签: javascript reactjs rendering