The Ultimate Guide to Adding Images in a React JS App

Posted by


Adding images to your React JS app is a common requirement for creating visually appealing and dynamic user interfaces. In this tutorial, I will guide you through the process of adding images to your React JS app step by step.

Step 1: Setting up your React JS app
First, make sure you have Node.js and npm installed on your machine. If not, you can download and install them from their official websites.

To create a new React JS app, open your terminal and run the following command:

npx create-react-app my-app

Replace my-app with the name of your app. This command will create a new React JS app with all the necessary dependencies and files.

Once the app is created, navigate to the app directory by running:

cd my-app

Now, start the development server by running:

npm start

This will open your app in the browser at http://localhost:3000.

Step 2: Adding images to your React JS app
To add images to your React JS app, you need to place the image files in the public directory of your app. Create a new directory inside the public directory called images and place your image files in this directory.

Next, you can reference these image files in your components using the public folder as the base path. For example, if you have an image called logo.png in the images directory, you can use it in your component like this:

import React from 'react';

const MyComponent = () => {
  return (
    <div>
      <img src={process.env.PUBLIC_URL + '/images/logo.png'} alt="Logo" />
    </div>
  );
};

export default MyComponent;

In the code above, we are using the process.env.PUBLIC_URL variable to reference the public folder in our app. By concatenating this with the path to the image file, we can display the image in our component.

Step 3: Styling images in your React JS app
You can style the images in your React JS app using CSS. You can apply styles directly to the image element using inline styles or create a CSS file and import it into your component.

To apply styles using inline styles, you can do the following:

import React from 'react';

const MyComponent = () => {
  return (
    <div>
      <img src={process.env.PUBLIC_URL + '/images/logo.png'} alt="Logo" style={{ width: '100px', height: '100px' }} />
    </div>
  );
};

export default MyComponent;

Alternatively, you can create a CSS file, for example styles.css, and style the images in that file:

img {
  width: 100px;
  height: 100px;
}

Then, import the CSS file into your component:

import React from 'react';
import './styles.css';

const MyComponent = () => {
  return (
    <div>
      <img src={process.env.PUBLIC_URL + '/images/logo.png'} alt="Logo" />
    </div>
  );
};

export default MyComponent;

Step 4: Handling image loading errors
In some cases, the image files may not load due to network issues or incorrect file paths. To handle image loading errors in your React JS app, you can use the onError event handler on the img element:

import React from 'react';

const MyComponent = () => {
  return (
    <div>
      <img
        src={process.env.PUBLIC_URL + '/images/logo.png'}
        alt="Logo"
        onError={(e) => { e.target.src = process.env.PUBLIC_URL + '/images/default.png' }}
      />
    </div>
  );
};

export default MyComponent;

In the code above, we are using the onError event handler to set a default image if the original image fails to load.

That’s it! You have successfully added images to your React JS app and styled them using CSS. By following these steps, you can create visually appealing and dynamic user interfaces in your React JS app.

0 0 votes
Article Rating
18 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@goodnessisaac9346
2 months ago

How did you highlight both img tag and create an indentation for both while still highlighted 3:13

@asikahmad507
2 months ago

Hey dear!
I from Bangladesh
Thank you for your video solved a problem for me!!

@MA718172
2 months ago

What an Amazing video. Solved my problem so easily and taught all the main points. Subscribed!

@user-vn8ud6zn1c
2 months ago

thank you

@nullzero9224
2 months ago

Best reference about this topic – Many Thanks 👋

@TheSano
2 months ago

thanks for the precise info with no bs what so ever. was very helpful. keep it up!

@TOMSIRGAMING
2 months ago

Bro! Am a beginner to JS & React — Is there any solution for this – if an image is imported with it path and later the path doesn't exist , I need to display a fallback/alternate image should display on that place as an error handling due to path doesn't exist/link broken! Can You HeLp Me in this💙

@meerav1119
2 months ago

Thank you so much sir I tryed many ways to add image but finally I got it by using this video it is so useful

@kronkner_8192
2 months ago

helped!! thanks a lot

@brandonojeda1686
2 months ago

Thanks very helpful!

@feedgaming8374
2 months ago

Thank u so much

@AshishKumar-cx4uv
2 months ago

Thanku

@vitorcorrea6305
2 months ago

Thank you for the content, really good work

@GoldenBharat2050
2 months ago

good video

@dobryden7196
2 months ago

short and informative, thank you

@43445fgfgd
2 months ago

huge thanks!!!

@dactal4444
2 months ago

you are great

@MuhammadBilal-eb1wz
2 months ago

thanks a loat sir love you😘