0

My goal is to display my elements URL property when i map on my array, i tryed to use 'require' according to other post on this website but for some reason it only works when i write directly the strings into my map.

i have the "images" folder into "src" folder. The file i display on bottom is in src/components thats why i used "../" but also tryed "./" just in case , still didnt work

this is my whole array i'd like to display properly:

const [products, setProducts]= useState([
        {key:1 ,name:'Structure Deck Assaut d Albaz VF',        price:'12,99', url:'../images/DSAlbaz.png'},
        {key:2 ,name:'Grands Créateurs Booster Box VF',         price:'84,91', url:'../images/grandscreateurs.png'},
        {key:3 ,name:'Coffret Arsenal Mystérieux VF',           price:'14,99', url:'../images/arsenalmyst.png'},
        {key:4 ,name:'La Bataille du Chaos Booster Box VF',     price:'84,99', url:'../images/battaillechaos.png'},
        {key:5 ,name:'Fantômes du passé saison 2 VF',           price:'24,99', url:'../images/fantome2nd.png'},
        {key:6 ,name:'Frères de Légende Booster Box VF',        price:'89,99', url:'../images/freresdelegende.png'},
        {key:7 ,name:'Coffret Or Maximum VF',                   price:'24,99', url:'../images/OrMaximum.png'},
        {key:8 ,name:'Coffret Or Maximum EN',                   price:'24,99', url:'../images/OrMaximum.png'},
    ])

this is my array maping that doesnt work as intended:

if(listToDisplay === 0){
        displayList = products.map(e =>{
            return<div className='product__card' key={e.key}>
                        <img className='product__picture' src={require(e.url)} alt='SD assaut d albaz'></img>
                        <h3>{e.name}</h3>
                        <p>{e.price}$</p>
                        <div className='buybutton'>Ajouter au panier</div>
                  </div>
        })}

error code : Uncaught Error: Cannot find module '../images/grandscreateurs.png' seems related to webpack from what i found

so i found another thread and tryed this, with import :

import Logo from '../images/grandscreateurs.png'

then tranform my object value like this :

{key:1 ,name:'Structure Deck Assaut d Albaz VF',        price:'12,99', url:Logo},

error message : Uncaught Error: Cannot find module '/static/media/grandscreateurs.7cf37936cedc1da9b626.png'

i am kinda lost now since i don't know how to interact with webpack very well and don't understand how to fix it when i read the thread. thanx you for your understanding and a possible step by step solution.

1
  • Don't require the url; just pass it directly. Commented Mar 21, 2022 at 15:13

1 Answer 1

1

If you already used the import, so don't use the require in img tag. Try this

import Logo from '../images/grandscreateurs.png'

...

<img className='product__picture' src={Logo} alt='SD assaut d albaz' />
Sign up to request clarification or add additional context in comments.

1 Comment

i cant directly use "Logo" as the src because since im maping an array and the <img> is in the map return i have to put dynamic value in here. it does works if i put 'Logo' in the 'url' property of my object too the set src={e.url}, but if i have 100 products it would force me import the 100 diff images at start of the file wich is not perfect i Believe^^. thanx for your anwser it helped since remooving the require still offer a solution even if i have to importm ultiple images.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.