英文:
How to display the image and title which are in array
问题
当我将以下内容放入React返回的JSX元素代码中时,三个图像和标题都会正确渲染:
<center>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/51PhUckHB3L.jpg"></img>
<div className="caption">asin_product_title<br></br>
<small className='ellipsis'>Electrolux EENB54EB Ultraenergica Classic Aspirapolvere</small>
<span className="tooltiptext">Electrolux EENB54EB Ultraenergica Classic Aspirapolvere</span>
</div>
</div>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/518Se4188mL.jpg"></img>
<div className="caption">right_product_title<br></br>
<small className='ellipsis'>Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</small>
<span className="tooltiptext">Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</span>
</div>
</div>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/518Se4188mL.jpg"></img>
<div className="caption">asinHeadline1<br></br>
<small className='ellipsis'>Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</small>
<span className="tooltiptext">Third Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</span>
</div>
</div>
</center>
现在我需要优化图像和标题的代码,就像从后端API中接收到以下图像URL或标题的数组或列表一样:
const imageUrlArray = ['http://ecx.images-amazon.com/images/I/51PhUckHB3L.jpg','http://ecx.images-amazon.com/images/I/518Se4188mL.jpg'];
const titleAttributeArray = ['asin_product_title','right_product_title'];
const titleNameArray = ['Electrolux EENB54EB Ultraenergica Classic Aspirapolvere',
'Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy']
所以根据imageUrlArray
的大小,我将不得不显示这些容器,而不是一个一个地硬编码它们。
我尝试的第一种方法:
{imageUrlArray.forEach((url, ind) => {
return (
<div className='container p-1'>
<img className='img' src={url}></img>
<div className="caption">{titleAttributeArray[url]}<br></br>
<small className='ellipsis'>{titleNameArray[url]}</small>
<span className="tooltiptext">{titleNameArray[ind]}</span>
</div>
</div>
);
})}
对于这段代码,我没有错误,但在我的网页上,上面的代码完全是空白的。请参考:
我想根据从后端接收到的数组来按需显示这些图像容器。有人可以帮助我实现这个吗?
英文:
When I put the below in the jsx element code of react return, the three images and titles are rendered correctly:
<center>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/51PhUckHB3L.jpg"></img>
<div className="caption">asin_product_title<br></br>
<small className='ellipsis'>Electrolux EENB54EB Ultraenergica Classic Aspirapolvere</small>
<span className="tooltiptext">Electrolux EENB54EB Ultraenergica Classic Aspirapolvere</span>
</div>
</div>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/518Se4188mL.jpg"></img>
<div className="caption">right_product_title<br></br>
<small className='ellipsis'>Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</small>
<span className="tooltiptext">Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</span>
</div>
</div>
<div className='container p-1'>
<img className='img' src="http://ecx.images-amazon.com/images/I/518Se4188mL.jpg"></img>
<div className="caption">asinHeadline1<br></br>
<small className='ellipsis'>Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</small>
<span className="tooltiptext">Third Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy</span>
</div>
</div>
</center>
This is how above code looks like:
Now I have to optimize the code of images and title, like from backend API I will be receiving an array or list of the URLs or images as follows:
const imageUrlArray = ['http://ecx.images-amazon.com/images/I/51PhUckHB3L.jpg','http://ecx.images-amazon.com/images/I/518Se4188mL.jpg'];
const titleAttributeArray = ['asin_product_title','right_product_title'];
const titleNameArray =['Electrolux EENB54EB Ultraenergica Classic Aspirapolvere'
,'Amore Legno Sforza segnalibro, segnalibro particolare, segnalibro legno, segnalibro laurea, idea regalo, segnalibri regali, regali originali per amiche, portafortuna da regalare, made in Italy']
So basis the size of the imageUrlarray
I would have to display these containers instead of hardcoding it one by one.
Approach 1 that I tried:
{imageUrlArray.forEach((url,ind)=>{
return(
<div className='container p-1'>
<img className='img' src={url}></img>
<div className="caption">{titleAttributeArray}<br></br>
<small className='ellipsis'>{titleNameArray}</small>
<span className="tooltiptext">{titleNameArray[ind]}</span>
</div>
</div>);
})}
I get no error for this but on my webpage, it is completely blank for the above code. Please see:
I want to display those image containers on demand based on array received from backend. Can someone please help me achieve this?
答案1
得分: 2
forEach()不返回任何内容,而在React中需要返回内容以使此行为生效。我认为你正在寻找的操作是map()。map的返回值是被渲染到你的DOM中的内容。
将forEach替换为map,其他逻辑仍将工作(除了在titleAttributeArray和titleNameArray中使用url作为索引的错误)。
查看这个问题,其中提供了一些关于它们之间的区别的出色解释!https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map
英文:
forEach() doesn't return anything which is required for this behavior to work in React. I think map() is the operation you are looking for. The returned value of the map is what is being rendered to your DOM.
Replace forEach with map and the rest of your logic will still work (except the bug where you use url as the index for the titleAttributeArray and titleNameArray)
See this question for some excellent explanations about the differences! https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论