0

i got a simple problem which i cant resolve, i got an "array" which contains and "array" where i want to display the first Picture,

heres the code:

                        {Skills.map((item, index) => 
                            <Window key={index} className="col-md-3 m-2 ">
                                <CardFrame className="cardFrame-max cardFrame-size-md">
                                    <div className="row justify-items-center">
                                        <div className="col-8 offset-2">
                                            <p className="battle-shonen-color">{"Skill Name: " + item.name}</p>
                                            <img src={require('../../' + item.effects[0].icon)}></img>
                                            <p className="battle-shonen-color">{"Skill Shortcode: " + item.shortcode}</p>
                                            <p className="battle-shonen-color">{"Skill Description: " + item.quote}</p>
                                        </div>
                                    </div>
                                </CardFrame> 
                            </Window>
                        )}

its at the tag where it shows me the error: "Error: Cannot find module './resources/img/icon/effects/Damage.png'" ,

so it does load the string saved in the array but doesnt add '../../' infront

Terraya
  • 121
  • 1
  • 8

3 Answers3

0

    {require(`../../${item.effects[0].icon}`)}
Hagai Harari
  • 2,767
  • 3
  • 11
  • 23
0

Can you try like this

 {Skills.map((item, index) => 
                            <Window key={index} className="col-md-3 m-2 ">
                                <CardFrame className="cardFrame-max cardFrame-size-md">
                                    <div className="row justify-items-center">
                                        <div className="col-8 offset-2">
                                            <p className="battle-shonen-color">{"Skill Name: " + item.name}</p>
                                            <img src={require(`../../${item.effects[0].icon}`)}></img>
                                            <p className="battle-shonen-color">{"Skill Shortcode: " + item.shortcode}</p>
                                            <p className="battle-shonen-color">{"Skill Description: " + item.quote}</p>
                                        </div>
                                    </div>
                                </CardFrame> 
                            </Window>
                        )}
 Run code snippet
Kesav
  • 151
  • 4
  • Already have tryed it, still getting same : Error: Cannot find module './resources/img/icon/effects/Damage.png' – Terraya Apr 02 '20 at 09:57
0

import your images in your items file. e.g. :

import ImageName from '../../whatever.jpg';

and use it in your items array.

[
  {
    icon: ImageName
  }
]
BeHappy
  • 3,705
  • 5
  • 18
  • 59
  • Yes that approach wouldnt be bad but since i load the images from a Database Object, i wouldnt do that since its not recommendet :s – Terraya Apr 02 '20 at 10:07
  • if you load it from database, How it is in your react project? if you load in your app to have to set address from built files. e.g. the images load in /img/ folder in production mode. – BeHappy Apr 02 '20 at 10:14
  • Yes well, lets say , the Files are Stored in the Project Localy but the Database Object have stored the "Path" to the Stored file – Terraya Apr 02 '20 at 10:16
  • so as I said, see where assets path is in production and set your path in DB base on it. – BeHappy Apr 02 '20 at 10:39