# Adding images
So how do we deal with images that we have locally. It’s really very easy, we just need to import and assign them to the source property of our image, like so:
So why does this work, well here is an explanation from the official docs:
. You can import a file right in a JavaScript module. This tells Webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This value is the final path you can reference in your code, e.g. as the src attribute of an image or the href of a link to a PDF.
To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a data URI instead of a path. This applies to the following file extensions: bmp, gif, jpg, jpeg, and png.
Importing Images With React

When developing web applications with React, we typically want to include visual elements to capture the users' interest. These visual elements could be any type of image, including JPGs, PNGs, SVGs, GIFs, and many others.
In this article, we will learn how to import images with React and see the various ways this could be achieved.
External images and local images are the two types of images we want to use in our React application. We are primarily concerned with local images in this article because external images do not require us to import them.
External images are images that have already been hosted externally and can be accessed by anyone via a URL, whereas local images are images that are only available to us on our local computer or project folder and that we want to use in our application.
How to Display Images hosted Externally
Before we get into how to import images, it's important to understand that images hosted elsewhere work the same way we've always used images in HTML — by adding the URL to the src attribute of the img tag:
Let's now learn how we can import (local) images with React!
How to Import Images with React
Apart from the external images, the way images are used in React differs significantly from that of other frameworks or even HTML. These images must first be imported into React before they can be used in our application.
This can be accomplished in two ways: by using the import statement or by using the require() function. We'll go over both approaches.
How to Import Images With React Using the import Statement
Because it is easier to read and understand, the import statement is the most commonly used method for importing locally stored images in React. The images are treated as default exports, and when we import them, we do so in the same way that we import components. This is done by specifying the relative path from the file to the image we are importing:
In the code above, we still use the img tag and src attribute, but this time the src attribute receives a variable rather than a string, which is passed using curly braces in JSX.
Note: We can import as many images as we'd like to, but each image must have a unique name in the import statement, else it will throw an error.
It is important to understand how to specify and obtain relative paths for files; otherwise, bugs and errors may occur. In the preceding example, the image was saved in an /src/images .
How to Import Images With React using the require() Function
The require() function is a Node.js function that is used to include external modules from files other than the current file. It works in the same way as the import statement and allows us to include images:
The only difference is the first line where we required the image via its relative path and then stored it in a variable which we accessed in the img tag via curly braces.
We can also decide to do this inline and avoid the extra line used to assign the image to a variable, as it's not compulsory:
Conclusion
We learned how to import local images using the import statement and the require() function in this article.
React js how to add an image — a beginners guide
![]()
A complete beginner's guide for adding and styling images in React js.
- Adding the image (the different ways)
- Styling the image
- Example Code
Adding the image
There are several different ways of inserting images in React.
- Using the image tag
Using the <img/> tag you will need to provide it with two values:
“src” (source): the URL or the path of the image.
“alt” (alternate): an alternate text in case of the image not being available.
- Setting a background image
Using CSS you can set the background of any element. This is most use when you want to display content over the image.
Adding an image from your computer
With the <img/> tag
Drag your image into your project and import it into the desired component. Afterward, you can insert the name of the import into the source property.
Inserting a local background image
Just like before you would import the image. Then you would insert it using CSS URL(…) or using inline styling as shown below.
Structuring your code
The correct way to structure images in your project is to add them in an “images” folder. If you are using other assets than just images, you might want to add all the assets folders into one, as shown below.
Example code:
Drag in the left sidebar to see the source code
Styling your image
The most useful styling properties for images include:
- Width and/or height: Setting the width and heights of the element
- Border-radius: Curving the edges of the element
- Filter: Learn more about filters…
- Opacity: Decimal value desciping the opacity of the element
<img/> specific:
- Vertical-align: Snapping the image verticly
background image specific:
- Background-image: Path or image URL
- Background-repeat: Setting image repetition for each or both axis
- Background-size: Setting the size of the image
- Background-position: Setting the position of the image in the background
This is my first post, so please leave a comment on what you thought about it so that i can improve and provide the best possible content!
Using the image tag in React
In React, image tags are a bit weird. This isn’t really React’s fault, but more a problem of where the images will reside on the server after the app is built.
I’m talking about the plain old <img src=""/> tag here. The same one you’d use in HTML.
When you img in a React component, the the src prop needs to be point at something that the server can serve.
Don’t Use a File Path From Your Computer
A common mistake for beginners is to set the src to a file path on their computer, like /Users/yourname/Projects/this-react-app/src/image.png . That won’t work.
Browsers are mostly sandboxed these days and won’t let you access files by their path on disk. If you did get that to work (maybe with file:// ), it’d break as soon as you deployed the app, because a web server won’t have that file in the same place! (And no, the solution is not to put it in the same place on the server 🙂
Two Ways to Include an Image in a React Component
With React, since there’s a build step, you need a way to include the image. There are 2 main ways to do that.
I’m going to assume a Create React App project here, where everything in the public directory gets copied to the server and everything under src is fair game for importing into JS files.
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.
Then you can reference it by that variable name. The name can be anything you want, it doesn’t have to match the image or anything.
Wherever you want to display the image, render your img tag and pass that variable as the src :
Note I’m using src=
Option 2: Put the image in the public directory
You can put the image file in the public folder (or if this is not Create React App… then any folder that will be copied to the server).
Then, assuming your server is treating the public folder as the “root” directory ( / ), then your images will be available relative to that – just like with plain HTML.
So if you had an image at public/images/thing.jpg , you could display that image this way:
Because this method makes the image available as a regular file on the web server, and you can test it by opening http://localhost:3000/images/logo.jpg in the browser (or, you know, your actual domain name, once it’s deployed!)
How Imported Images Work in React
First, know that import s are not handled by React at all – they’re handled by your bundler, which is probably Webpack. (if you’re using Create React App, it is definitely Webpack)
Webpack, Rollup, Parcel, and other bundlers all work conceptually the same way: when you import a static file, like an image or a CSS file, the bundler doesn’t literally paste that file in at the import location. Instead, it makes a note that this particular JS file depends on this particular image/CSS file/whatever.
Then the bundler will copy the image to the output directory with a generated unique name (like a5c8d3f89cad.jpg ) and, behind the scenes, it will replace <img src=
If the image is especially small, Webpack might even decide to inline it into the JS bundle, as an optimization.
This all happens without you having to worry about it.
The Best Way to Use the img tag in React?
For one-off images that are related to the component at hand, I like to import them. Imported images have the side benefit that, if the file is missing, the build will fail, and you’ll find out quick! For that reason, I lean toward importing an image if I’m going to use it.
For generic site-wide images, or where it would be annoying to manually import them, I’ll put them in public. This is especially useful when the React app is only a small slice of your overall site, and the same image should be used by both React and other non-React pages. In this case I’d rather avoid duplicating images (with the potential that the copies get out of sync).
Learning React can be a struggle — so many libraries and tools!
My advice? Ignore all of them 🙂
For a step-by-step approach, check out my Pure React workshop.

Learn to think in React
- 90+ screencast lessons
- Full transcripts and closed captions
- All the code from the lessons
- Developer interviews
Dave Ceddia’s Pure React is a work of enormous clarity and depth. Hats off. I’m a React trainer in London and would thoroughly recommend this to all front end devs wanting to upskill or consolidate.