I am having a heck of a time trying to load dynamic images while mapping in the return statement. If the image is static, no probablems.
This works:
src={require('../../assets/user-images/image-anne.jpg')}
However, I am mapping through a list of comments and need to display the images dynamically from the path given in the image path of the props.
The image prop looks like:
‘./assets/user-image/image-anne.jpg’
So I tried the following but this does not work:
src={require(`../.${image}`)}
I get the following:
“Cannot find module ‘./../assets/user-images/image-anne.jpg”
I also try just using {image} but it doesn’t seem to know to look for the assets folder under src.
I am trying to load the images in a component under src/components/feedbackList
The images are in src/assets/user-images
If I go directly for the assets folder is gives me the standard "/assets falls outside of the /src directory."
Any direction is appreciated.
2
Answers
Here is an example code for dynamically changing
img
srcFollow this tips to able to load image.
If not working above the tips then use this one.
src={require("../." + image).default}