What I am trying to accomplish is importing each file from the proper directory. My backend code saves files to the images directory, and my frontend directory is where I hold my React.js code.
The structure of my directory can be seen below.
-- Backend
-- backend code
-- Frontend
-- frontend code
-- images
-- user_1
-- People.jpg
-- user_2
...
As you can see, I am getting the path from the backend. imagePath will look something like this "../images/user_1/People.jpg". I saw another stackoverflow post store the image in const variable using the require keyword, but I received an error using that method.
@observer
export default class SnapCapsule extends React.Component {
componentDidMount() {
axios.get(`${this.props.domain}/snapcapsule`)
.then(res => {
res.data.map((capsule) => {
var domainSize = this.props.domain.length+13;
var imagePath = "../" + capsule["image"].substring(domainSize);
var dateToPost = capsule["dateToPost"].substring(0, 10);
var username = capsule["username"];
console.log(capsule["caption"], imagePath, dateToPost, username);
this.props.store.addCapsule(
capsule["caption"],
imagePath,
dateToPost,
username
)
})
});
}
componentWillUnmount() {
this.props.store.clear();
}
render() {
const { capsules } = this.props.store;
const capsuleList = capsules.map((capsule, i) => (
<Col key={i} xs={6} md={4}>
<Thumbnail src={capsule.imagePath} alt="242x200">
<h3> {capsule.title} </h3>
<p> @{capsule.username} </p>
</Thumbnail>
</Col>
));
return (
<Grid>
<Row>
{ capsuleList }
</Row>
</Grid>
);
}
return (
<Grid>
<Row>
{ capsuleList }
</Row>
</Grid>
);
}
}
Answer
Instead of providing a path to the image tag in the directory. I configured the backen to be retrieved photos through urls.
capsule.imagePath => http://localhost:8000/snapcapsule/images/user_1/People.jpeg