0
I have a function map Javascript where I return all the photos of a certain record. Among these photos, one of them has to receive a text, which in my case is Main photo, which will always be the first element of the array. The question is that the text always goes to all images, and I have no idea how to put the text only in the first photo.
Below is the code in question.
<ul>
{fotoApi?.fotos?.map((foto, index) => (
<li key={index} style={{ listStyle: "none" }}>
<img
style={cssListImg}
src={fotoApi.fotos[index]}
onClick={() => {
setFile(fotoApi.fotos[index]);
setImg(null);
setI(index);
setImgSel(foto);
}}
/>
<br />
{/* fotoApi.fotos[0] ? <>Foto principal</> : <>""</> */}
{/* A linha acima é o que eu tentei, mas sem sucesso */}
</li>
))}
</ul>
As requested by @Virgilionovic, my "photos" are created as follows:
const [fotoApi, setFotoApi] = useState({fotos: [], caminhoimagem: ""}];
first you have to do outside the
map
! and then make aslice
to skip the first image!– novic
has other errors as well, case your example of the object photos?
– novic
You can put a
if (index !== 0) return dentro desse
onClick`... but @Virgilionovic’s suggestion is more accurate. Question: all images are clickable?– Sergio
@Sergio, yes, they all are, and should be clickable
– augusto francisco
@Virgilionovic I’ll try what you sent here ;)
– augusto francisco