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.

8
  • I think its because of the require. Commented Jul 28, 2018 at 7:21
  • @JaisonJustus How? Commented Jul 28, 2018 at 7:22
  • Like this - <img src="/img/hand.png" width="70" height="50" alt="cam"/> ? Commented Jul 28, 2018 at 7: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 Commented Jul 28, 2018 at 7:26
  • 1
    If you import the image (e.g. import imgVar from ' ./img/hand.jpg') and then write <img src={imgVar}/> 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 <img src> Commented Jul 28, 2018 at 8:30

6 Answers 6

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>
        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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"/>

8 Comments

This picture is in the same sub-folder of src as my js file.
Do you use create-react-app to create your app? wepack has a limit for the size of the pic. If the pic exceed the size in the config file,then it will not be packed. Maybe you can check it.
Oh. What's the limit in size?
I found that the limit of size is 2MB, but my picture is only 39KB.
Try this. stackoverflow.com/questions/34582405/…. It is the same problem with you.
|
1

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

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

Comments

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

7 Comments

I tried this and the result is the same. It doesn't work for me. Still, no error message.
Yes. If the path is not correct, there will be an error message. I tried.
can you put the image in the public folder and try again,i will edit the code
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 I have added a working sample in codesandbox,please do check
|
0

Put your images into your public folder.

Comments

0

I know two ways, from the component or from the public folder.

  • A - From the component: you use CSS or Imports

import:

import img1 from "./assets/images/img.svg" 

CSS:

.img {
background-image: url("./assets/images/img.svg");
background-size: contain; /* Adjust the size of the image */
width: 10em;
height: 10em;
}
  • B - From the public folder Then you can use

    <img src="/assets/images/img.svg"/>
    

Which of the two is better?, neither.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.