6

I have a local image that I want to put on my webpage built in react. I looked at this but it didn't help me. My code is as following:

const right={
    float:"right",
    fontSize:"18px"
}

export default class CloseUpCam extends Component {
render() {
    return (
        <div className="closeupcam" style={right}>
            <div>CLOSEUP CAMERA</div>
            <img src={require('./img/hand.png')} width="70" height="50" alt="cam"/>
        </div>
    )}}

There's no error messages. The picture just doesn't show up. Does anyone know what's wrong? Thanks.

Young
  • 739
  • 3
  • 9
  • 12
  • I think its because of the require. – Jaison Justus Jul 28 '18 at 07:21
  • @JaisonJustus How? – Young Jul 28 '18 at 07:22
  • Like this - `cam` ? – Jaison Justus Jul 28 '18 at 07:24
  • @JaisonJustus That's definitely not working. I believe I need that require because of webpack. I used Vuejs before and I needed that require for adding images to my webpage. I just don't know what I did wrong with react – Young Jul 28 '18 at 07:26
  • 1
    If you import the image (e.g. import imgVar from ' ./img/hand.jpg') and then write does it help? If not, change the image name to a non existent image. Do you get an error? If you do, I guess that it means that the first import worked and the problem is in the – Yossi Jul 28 '18 at 08:30
  • @Rahamin It doesn't help to change to import. And yes it does give an error if I used a non-existant image name. How do I fix it? – Young Jul 28 '18 at 08:35
  • So the problem is not in the import command, but in the img src... strange. No idea. Sorry – Yossi Jul 28 '18 at 08:48
  • The only idea I have is to start a new project with create-react-app and insert there the image and this component only, maybe it works. Your code seems fine. – Yossi Jul 28 '18 at 10:29

5 Answers5

9

In react you must import your image file like all other files

import Image from "./img/hand.png";

const right = {
    float: "right",
    fontSize: "18px"
};

export default class CloseUpCam extends Component {
    render() {
        return (
            <div className="closeupcam" style={right}>
                <div>CLOSEUP CAMERA</div>
                <img src={Image} width="70" height="50" alt="cam"/>
            </div>
        );
    }
}
sus
  • 28
  • 4
Neck
  • 91
  • 1
  • 3
1

your way of show image actually do work in React,I think its the matter of your path of the pic.Do you put all your static files under the public folder,or how do you manage your static pages?

<img src={require('./img/hand.png')} width="70" height="50" alt="cam"/>
Thomas.lin
  • 430
  • 1
  • 5
  • 11
1

Put the images in the public folder of react app and try

<img className="" alt="Tag" src="/assets/images/tag.svg"/>

0

Put the images in the public folder of react app and try

    const right={
        float:"right",
        fontSize:"18px"
    }

    export default class CloseUpCam extends Component {
    render() {
        return (
            <div className="closeupcam" style={right}>
                <div>CLOSEUP CAMERA</div>
                <img src="/imagename.jpg" width="70" height="50" alt="cam"/>
            </div>
        )}}

Here is a working sample

Harikrishnan
  • 1,097
  • 8
  • 13
  • 1
    I tried this and the result is the same. It doesn't work for me. Still, no error message. – Young Jul 28 '18 at 07:38
  • Yes. If the path is not correct, there will be an error message. I tried. – Young Jul 28 '18 at 07:39
  • can you put the image in the public folder and try again,i will edit the code – Harikrishnan Jul 28 '18 at 07:39
  • I got an error: failed to compile. Module not found: You attempted to import ../../../public/hand.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/. – Young Jul 28 '18 at 07:44
  • @Young I have added a working sample in codesandbox,please do check – Harikrishnan Jul 28 '18 at 07:44
  • @Young you don't need to import image if it is put in public folder – Harikrishnan Jul 28 '18 at 07:51
  • It's the same, the picture still doesn't show up even if I put it in the public folder. – Young Jul 28 '18 at 08:00
0

Put your images into your public folder.