I am new to ReactJS and I can't seem to figure out where to "require" the images I need.
import React, { Component } from 'react';
class ClothesFrame extends Component{
state = {
/*clothesimages: { //this works
hat: require("./img/hat.svg"),
scarf: require("./img/scarf.svg")
},*/
outfit: require('./outfit.json'),
clothesimages: (function() //this does not
{
let outfit = require('./outfit.json');
let result = {};
for(var garment in outfit)
{
for(var prop in outfit[garment])
{
if(prop == "img")
{
result[garment] = require(outfit[garment][prop]); //Cannot find module './img/hat.svg'
}
}
}
return result;
})()
}
constructor(props){
super(props);
this.getClothes = this.getClothes.bind(this);
this.getClothesStr = this.getClothesStr.bind(this);
this.isInBoundaries = this.isInBoundaries.bind(this);
}
I get the error "Cannot find module './img/hat.svg'" when I try to load the images from a path specified in a JSON file. The first clothesimages works just fine but the second gives me the error. I want to be able to load the images from a path in a JSON file rather than in the code itself. My only incling is that the value of the second clothesimages is actually worked out later than it otherwise would be.