Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link image in react.js using source

I am working on a react.js website and I want to link a image (logo) to the website. See folder structure

enter image description here

My image code for the footer logo is

<img className="img-responsive" src="public/assets/image/img/footer-logo.jpg" alt="logo"/>

But the image is not visible on the website when I run it. Any solutions??

like image 975
kavin Rakshan Avatar asked Nov 27 '16 22:11

kavin Rakshan


People also ask

How do I use an image source in React?

Option 1: import the image into the component Put the image file somewhere under the src folder. This alone will not automatically make it available, so you have to import the image into the React component where you're using it. import companyLogo from './path/to/logo.

How do I link an image URL in React?

To use an image as a link in React, wrap the image in an <a> tag or a Link tag if using react router. The image will get rendered instead of the link and clicking on the image will cause the browser to navigate to the specified page.

How do I fetch images from API in React?

To fetch image from API with React, we can use the fetch function. We call fetch with the imageUrl to make a GET request to it. Then we call res. blob to convert the response object to a blob.

Do images go in src or public React?

In short: Any images that you import in your React components should be stored close to where they are used (preferably in the same directory). Any images that you do not import in your React components (e.g. favicons ) should be stored in your public/ directory, e.g. under a favicons/ folder.


2 Answers

If you are using webpack and assuming you are in one of the components as displayed in the image then do:

import logo from '../../public/assets/image/img/foorter-logo.jpg';
<img className="img-responsive" src={logo} alt="logo"/>

If you want to stick to what you have then you can do:

<img className="img-responsive" src="../../public/assets/image/img/footer-logo.jpg" alt="logo"/>
like image 173
Kafo Avatar answered Sep 22 '22 21:09

Kafo


You image src url need to be relative to the file you are using it in. So You need to use it like ../../public/assets/image/img/footer-logo.jpg.

Also in react you need to enclose your source within curly brackets

<img className="img-responsive" src={"../../public/assets/image/img/footer-logo.jpg"} alt="logo"/>

Also if you are using webpack you need to use require with the image src url like

<img className="img-responsive" src={require("../../public/assets/image/img/footer-logo.jpg")} alt="logo"/>

I hope it helps :)

like image 41
Shubham Khatri Avatar answered Sep 21 '22 21:09

Shubham Khatri