Featured Article

How to Add Image in React JS Step by Step Guide w3schools

Adam Jul 13, 2026

React, a popular JavaScript library for building user interfaces, often requires the integration of images. In this guide, we'll explore how to add images in React, from basic implementation to handling different image sources and managing image loading.

CRUD Operation in React JS with Source Code
CRUD Operation in React JS with Source Code

Before we dive in, ensure you have Node.js, npm, and Create React App installed on your system. Let's start by creating a new React project: `npx create-react-app my-app` and navigate into your new project: `cd my-app`.

5 Steps for Learning React JS in 2022: A Blueprint - Travis Media
5 Steps for Learning React JS in 2022: A Blueprint - Travis Media

Importing and Displaying Images

First, let's import and display an image from a local folder or a URL.

How to build your first React app
How to build your first React app

$cd into your project directory and create a new folder named 'images'. Inside 'images', create a new file called 'sample.jpg'.

Importing a Local Image

Learn React JS for Beginners || React JS Tutorial for Beginners 2026 | Intro to React
Learn React JS for Beginners || React JS Tutorial for Beginners 2026 | Intro to React

Now, let's import and display this image in our App.js file:

```jsx import React from 'react'; import './App.css'; import sample from './images/sample.jpg'; function App() { return (

Sample Image
); } export default App; ```

$Notice that we're using curly braces {} to import the image as a JavaScript object. Also, we've provided an alt attribute for accessibility.

Importing a Remote Image (CDN)

How to Easily Fetch and Display JSON in React
How to Easily Fetch and Display JSON in React

To import an image from a URL (e.g., a CDN), use the src attribute directly:

```jsx Random Image ```

Here, we're using the picsum.photos service to load a random image. Ensure to include appropriate alt text for accessibility.

Handling Image Sources Dynamically

a laptop computer sitting on top of a desk next to a window with the words 23 react projects with source code
a laptop computer sitting on top of a desk next to a window with the words 23 react projects with source code

In many cases, you'll need to handle image sources dynamically, such as displaying different images based on user interactions or async data fetching.

To do this, you can use JavaScript variables to store image sources:

Read Everything About React JS | Click the Link below!
Read Everything About React JS | Click the Link below!
Learn React JS - Class Based - New user project - To Do list
Learn React JS - Class Based - New user project - To Do list
React Projects That Can Help You Get Hired
React Projects That Can Help You Get Hired
How to Set Up a React App With Vite
How to Set Up a React App With Vite
How to Create Your First React App With JavaScript
How to Create Your First React App With JavaScript
Style Your React Apps Like a Pro! 💻✨
Style Your React Apps Like a Pro! 💻✨
an image of a man with the text learning react today i learned react core day - by - day progress log
an image of a man with the text learning react today i learned react core day - by - day progress log
an advertisement for the internet with many different things on it, including pictures and text
an advertisement for the internet with many different things on it, including pictures and text
How to build a student task management system in react (step by step)
How to build a student task management system in react (step by step)
Photopea How To Add Shadow
Photopea How To Add Shadow
How to Create a React App in 2024
How to Create a React App in 2024
Learn React in 5 minutes - A React.js tutorial for beginners
Learn React in 5 minutes - A React.js tutorial for beginners
Build Your Own Self Improving AI Wiki in 11 Minutes (Free Skill)
Build Your Own Self Improving AI Wiki in 11 Minutes (Free Skill)
an image with text that reads octostudio lesson 3 when to start? how to use trigger blocks to make things react
an image with text that reads octostudio lesson 3 when to start? how to use trigger blocks to make things react
Free, Ready to Use, Scratch Lesson!
Free, Ready to Use, Scratch Lesson!
22+ React Developer Tools to Increase Your Programming Productivity [Updated 2023]
22+ React Developer Tools to Increase Your Programming Productivity [Updated 2023]
a kid is sitting at a desk with a laptop and pointing to the screen that says,
a kid is sitting at a desk with a laptop and pointing to the screen that says,
How to use My Blocks in Scratch | Custom Blocks Tutorial in Scratch 3.0 - Kodex Academy
How to use My Blocks in Scratch | Custom Blocks Tutorial in Scratch 3.0 - Kodex Academy
How to Make a Scrolling Background in Scratch | Moving Sprite in Scratch - Kodex Academy
How to Make a Scrolling Background in Scratch | Moving Sprite in Scratch - Kodex Academy
an animation flower with the text, cursor selection animation tutor
an animation flower with the text, cursor selection animation tutor

```jsx import React, { useState } from 'react'; function App() { const [imageSrc, setImageSrc] = useState('https://picsum.photos/id/237/200/200'); const changeImage = () => { setImageSrc('https://picsum.photos/id/238/200/200'); }; return (

Random Image
); } export default App; ```

In this example, we're using React's useState hook to manage the image source. Clicking the 'Change Image' button will update the image source.

Image Loading Placeholders and Optimizing Performance

Loading images can affect your app's performance. To improve user experience, consider using loading placeholders and optimizing images.

Using Loading Placeholders

By default, browsers display a default loading animation. However, you can customize the loading placeholder by using your own gif or svg images:

```jsx import React, { Suspense } from 'react'; import { Spin ] from 'antd'; // or loading spinner library of your choice const MyImage = React.lazy(() => import('./images/sample.jpg')); function App() { return (

}>
); } export default App; ```

Here, we're using React's Suspense component with the fallback prop to display a loading spinner while the image is being loaded.

Optimizing Images

Optimizing images can significantly improve your app's performance. Consider using tools like ImageOptim or online services like TinyPNG to reduce file sizes. Additionally, you can use third-party libraries like react-image to lazy-load and preload images.

Remember, adding images to your React project doesn't have to be complex. With a little practice, you'll master the art of displaying, managing, and optimizing images in no time. Happy coding!