1

I am working on a react project. I am trying to add an image to one of my components. I made a directory named "images" in my project structure tree and I have put my image in this directory (image of project structure is attached below). I want to pass this image as src to my img tag. For this I right clicked on image and selected "copy image path" and then pasted that path to src of img tag. But when I try to run it, I get an error saying "Failed to load resource: the server responded with a status of 404 (Not Found)". Here is my code of component and screenshot of project structure.

P.S. I am on Ubuntu Environment

export default class CustomizedAppComponent extends Component {
    render(){
        return(
            <div>
                <img src="/home/user/Documents/Tessact-Master/dev/js/images/logo.png"/>
            </div>
        );
    }
}

Project Structure

3
  • try src='./dev/js/images/logo.png' Commented Nov 2, 2016 at 12:35
  • That looks like a system path from root. React is a server and therefore uses the http protocol from the root of the react project. Commented Nov 2, 2016 at 12:36
  • How do you serve your app? And how do you preview it in the browser? Commented Nov 2, 2016 at 12:48

2 Answers 2

2

No Need to use ../ or require in your url.. SIMPLE TRICK: It is very important from where you are serving you app. If you are serving your app from Tessact-Master then you code should be like this below

use just /dev not ./dev

export default class CustomizedAppComponent extends Component {
    render(){
        return(
            <div>
                <img src={"/dev/js/images/logo.png"}/>
            </div>
        );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This Worked. Thanks
@user3761761 It is good to upvote a perfect answer for your solution. It helps the community.
0

First of all the src of the image must be relative to the location of the component it is used in. Assuming the CustomizedAppComponent to be in the components or containers folder your image path must look like

 "../images/logo.png"/

if the component is again inside another folder inside folder components, then give path as

"../../images/logo.png"/             // add '../' so on 

Also in react you may need to mention the src within a require if you are using webpack to transpile your jsx and if not and then omit require in the below code.

export default class CustomizedAppComponent extends Component {
    render(){
        return(
            <div>
                <img src={require("../images/logo.png")}/>
            </div>
        );
    }
}

5 Comments

You shouldn’t need to use the require. That will crash your JS.
@JF Why will it crash my JS. I don't see it happening in my case.
If you require() an image file, there will be an invalid syntax error because the binary file is not valid JS. require() loads the file at the given path, executes it, and returns the module.exports object.
@JF I have been using this syntax for a long time and never faced any issue with ti
@JF have a look at this SO question stackoverflow.com/questions/34582405/…. OP did not specify explicitly what he is using to compile the JSX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.