Trying to import image into a React component. It works if I reference the file directly. But not if I call it from an object. I have tried with the require() and using {} variations but just guessing now.
This works:
import React from 'react';
import './Thumb.css';
const thumbObject = {
imageSrc: './Angelica05.jpg',
filename: 'Angelica05.jpg'
}
class Thumb extends React.Component {
render () {
return (
<div className="thumbbox">
<div className="thumbimage">
<img src={require('./Angelica05.jpg')} alt=''/>
</div>
<h2>{thumbObject.filename}</h2>
</div>
)
}
}
export default Thumb;
This doesn't work
import React from 'react';
import './Thumb.css';
const thumbObject = {
imageSrc: './Angelica05.jpg',
filename: 'Angelica05.jpg'
}
class Thumb extends React.Component {
render () {
return (
<div className="thumbbox">
<div className="thumbimage">
<img src={require(thumbObject.imageSrc)} alt=''/> //This is the line I can't get working.
</div>
<h2>{thumbObject.filename}</h2>
</div>
)
}
}
export default Thumb;