Display an image from a URL or Local Path in React

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Table of Contents

  1. Display an image from a URL in React
  2. Display an image from a local path in React
  3. Display an image from the public directory in React

# Display an image from a URL in React

To display an image from a URL, use the img tag and set its src prop to the complete URL of the image.

Optionally set the alt prop to a short description of the image.

App.js
import React from 'react'; export default function App() { return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }

display image from url in react

The code for this article is available on GitHub

The example shows how to display an image from an external URL.

We used the img tag and set its src prop to the complete URL that points to the image.

Notice that the image that is self-closing - <img />.

The alt prop helps screen readers understand what the image is about.

# Display an image from a local path in React

If you need to display an image from a local path in React:

  1. Download the image and move it into your src directory.
  2. Import the image into your file, e.g. import MyImage from './thumbnail.webp'.
  3. Set the src prop of the image element to the imported image.
App.js
import React from 'react'; import MyImage from './thumbnail.webp'; export default function App() { return ( <div> {/* ๐Ÿ‘‡๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐Ÿ‘‡๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }

display image from local path in react

The code for this article is available on GitHub

The example assumes that you have an image named thumbnail.webp in the same folder as the App component.

Make sure to specify the correct path to the image file (including the extension).

For example, if you are importing an image from one directory up, you would import as import MyImage from '../thumbnail.webp'.

The image has to be located in the src directory of your project.

Notice that in both examples we set the src prop on the img tag in order to render the image.

With images from an external URL, you set the src prop of the image to the complete URL.

With local images, you set the src proper of the img tag to the imported image.

Depending on your setup, you might also be able to use the require() syntax to display local images.

App.js
import React from 'react'; export default function App() { return ( <div> {/* ๐Ÿ‘‡๏ธ local image */} <img src={require('./thumbnail.webp')} alt="horse" /> </div> ); }

The code sample assumes that there is a thumbnail.webp file located in the same directory as your App.js module.

You can also check out my detailed guide on how to import an image in React.

# Display an image from the public directory in React

You can also render images that are located in the public directory of your React project.

When your images are located in the public directory, use an absolute path.

The following example assumes that there is a thumbnail.webp image located under public/images/thumbnail.webp.

App.js
import React from 'react'; export default function App() { return ( <div> <img src="/images/thumbnail.webp" alt="horse" /> </div> ); }

display image from public directory in react

The code for this article is available on GitHub

Notice that we specified an absolute path to the image - one that starts with a forward slash.

path
public/ images/ thumbnail.webp

The public folder is omitted and the path starts at the images directory.

Make sure to specify the extension of the image in the value of the src attribute.

I've also written a detailed guide on where you should store images in a React app.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev