I'm getting an error when I try to set the src
of an image using props
.
I searched a lot for a solution to this problem, but all of them, brings me to the require()
, and then set the props
inside it. But, it's not working to me.
The image path it's correct, because when I use the import
method to set the src
of an image, it works. But I need lots of "ProjectCard()
" components, and the props
solution is better.
Component that contains the image:
import React from 'react';
const ProjectCard = (props) => (
<div>
<img src={ require(props.image).default } />
</div>
);
export default ProjectCard;
Component that it's importing the ProjectCard():
import React from 'react';
import ProjectCard from './components/ProjectCard/';
const App = () => (
<ProjectCard image="../../images/image.jpg" />
);
export default App;
I've tried this solution too, but didn't go well (using single quotes):
<ProjectCard image="'../../images/image.jpg'" />
Both tentatives, I get the same error:
Error: Cannot find module '../../assets/image.jpg'
I used create-react-app.