最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set image src with props - Stack Overflow

programmeradmin1浏览0评论

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()" ponents, 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 './ponents/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.

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()" ponents, 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 './ponents/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.

Share Improve this question asked Jun 17, 2021 at 7:24 Eduardo DornelesEduardo Dorneles 1971 gold badge3 silver badges13 bronze badges 5
  • It's probably because you're using relative paths, and the ponent that you're passing to isn't in the same hierarchy (it's too shallow or too deep). Use absolute paths to alleviate that. ;) – Joel Hager Commented Jun 17, 2021 at 7:26
  • 1 Ok! I'm gonna try to use absolute path, and then I'll give the feedback! – Eduardo Dorneles Commented Jun 17, 2021 at 7:28
  • 1 You can import that image into parent ponent, in your case App like this import Diamond from '../../images/image.jpg'; and use it <ProjectCard image={Diamond} /> – mohit jain Commented Jun 17, 2021 at 7:35
  • Yeah, with this, works fine. But, how I said, I need lots of "ProjectCard()" ponents, and the props solution is better. – Eduardo Dorneles Commented Jun 17, 2021 at 7:43
  • @mohitjain is correct. Import the images and pass them as a prop to ProjectCard ponent. The alternative is to move the images into the public folder and then specify the paths from there. – Drew Reese Commented Jun 17, 2021 at 7:44
Add a ment  | 

1 Answer 1

Reset to default 7

PUBLIC FOLDER

Considering you have a public folder structured as followed,

- public
   |- images
       |- image.jpg

You can structure your ponents as such:

const App = () => (
    <ProjectCard image="image.jpg" />
);
const ProjectCard = (props) => (
    <div>
        <img src={`images/${props.image}`} />
    </div>
);

Where your image path will simply be absolute to the public folder, as images/image.jpg


SRC FOLDER

On the other hand, if you have your images inside your src folder as followed,

- src
   |- ponents
       |- ProjectCard.jsx
   |- images
       |- image.jpg
   App.jsx

You can structure your ponents as such:

import image from "./images/image.jpg";

const App = () => (
    <ProjectCard image={image} />
);
const ProjectCard = (props) => (
    <div>
        <img src={props.image} />
    </div>
);
发布评论

评论列表(0)

  1. 暂无评论