I followed this post to require images into my un-ejected create-react-app for Webpack: React won't load local images
Without the above an error is thrown.
The issue is that when doing this in a mapping function it breaks down:
const ThumbNail = styled.img`
border: 2px dotted blue;
`;
export default props => {
const images = [
'../images/dragonglass_pendant.jpg',
'../images/dragonglass_pendant2.jpg',
'../images/dragonglass_pendant3.jpg',
];
return (
<React.Fragment>
{
images.map(imageSrc => {
console.log(imageSrc, `=====imageSrc=====`);
return <ThumbNail src={require(imageSrc)} />
})
}
</React.Fragment>
)
}
// Chrome browser console
../images/dragonglass_pendant.jpg =====imageSrc=====
ImageThumbnails.jsx:25 ../images/dragonglass_pendant.jpg =====imageSrc=====
catalog sync:2 Uncaught Error: Cannot find module '../images/dragonglass_pendant.jpg'
at webpackEmptyContext (eval at ./src/catalog sync recursive (main.chunk.js:55), <anonymous>:2:10)
at eval (ImageThumbnails.jsx:26)
at Array.map (<anonymous>)
at eval (ImageThumbnails.jsx:24)
at renderWithHooks (react-dom.development.js:13354)
at mountIndeterminateComponent (react-dom.development.js:15407)
at beginWork (react-dom.development.js:16042)
I have also tried not requiring. This fails
I've also tried using the import keyword at the top. This works but removes programmatic image imports (images must import based on API call data)
Does anyone know why the require breaks above when mapped over?
EDIT: Using the import keyword in place of require I get the same error for each image:
catalog lazy groupOptions: {} namespace object:5 Uncaught (in promise) Error: Cannot find module '../images/dragonglass_pendant.jpg'
at eval (eval at ./src/catalog lazy recursive (main.chunk.js:55), <anonymous>:5:11)