So I'm working on a React project and I have a json file with several elements. I'm trying to map over the array from that json file and output the name and image. There's an image folder in the same directory. Basically my issue is that react isn't able to find those images even after providing a relative path. I found something on stackoverflow with this code.
function importAll(r) {
let images = {};
r.keys().map((item, index) => { images[item.replace('./', '')] = r(item); });
return images;
}
const images = importAll(require.context('./pics', false, /\.(png|jpe?g|svg)$/));
The issue with this is it gets all the images but they have a weird letter/number attached making it so I can't have the json file name/path along with that image. Ex: /static/media/name.a4g6s77.jpg. The other option is to import each image such 'import name from './pics/name.jpg';' Then the issue is I'm getting the json back as a string and I would need to access name as a variable and not a string. I've tried Convert string to variable name in JavaScript but it didn't help maybe because it's in jsx? Any suggestions?