Import image dynamically in React component

I used context.

const images = require.context('../../../assets/img', true);
loadImage = imageName => (assets(`./${imageName}`).default);
<img src={loadImage("someimage.png")} alt="" />

I don’t know if this is an optimal solution, but it works.

Leave a Comment