When working with images in React, there are a few important things to understand, like where to place your images, how to import them, and how to display them inside your components. Let's walk through everything step-by-step.
First, it's important to know where to store your images in a React project. By default, when you create a new React project, you'll see a folder called public
and a folder called src
.
For most cases, you’ll want to store your images in the src
folder, because you’ll likely use them in your components.
To demonstrate how to work with images in React, let's walk through the process of adding an image to a component.
First, copy an image into your project’s src
folder. For example, you can create a new folder inside src
called images
and place your image there.
Let’s say you added an image called logo.png
to the src/images
folder.
In React, to use an image in your JSX code, you first need to import it. Here’s how you can import the image into a component:
import React from 'react';
import logo from './images/logo.png'; // Adjust the path based on where your image is stored
const App = () => {
return (
<div>
<h1>Welcome to My React App</h1>
<img src={logo} alt="App Logo" />
</div>
);
};
export default App;
import logo from './images/logo.png';
tells React to import the image file so we can use it inside the component.<img>
tag and set the src
attribute to the image we imported.alt
attribute will be shown instead. It’s also helpful for screen readers.Sometimes, you might want to store your images in the public
folder. This is useful when you want your images to be available to the browser directly, without having to import them in your components.
Let’s say you have an image called banner.jpg
that you place directly inside the public
folder.
When you place images in the public
folder, you don’t need to import them. You can reference them by their URL relative to the public
folder, like this:
import React from 'react';
const App = () => {
return (
<div>
<h1>Welcome to My React App</h1>
<img src="/banner.jpg" alt="Website Banner" />
</div>
);
};
export default App;
Sometimes, you might want to display images based on certain conditions or data. In these cases, you can use dynamic image paths. Here's an example:
import React from 'react';
import apple from './images/apple.png';
import banana from './images/banana.png';
const App = () => {
const fruits = [
{ name: 'Apple', image: apple },
{ name: 'Banana', image: banana },
];
return (
<div>
{fruits.map((fruit, index) => (
<div key={index}>
<h2>{fruit.name}</h2>
<img src={fruit.image} alt={fruit.name} />
</div>
))}
</div>
);
};
export default App;
You can style images in React in two ways:
<img src={logo} alt="App Logo" style={{ width: '200px', height: 'auto' }} />
Create a CSS file (e.g., App.css
) and add some styles for your image:
/* App.css */
.image-logo {
width: 200px;
height: auto;
border-radius: 10px;
}
Then, import the CSS file and apply the class to your image:
import React from 'react';
import './App.css';
import logo from './images/logo.png';
const App = () => {
return (
<div>
<h1>Welcome to My React App</h1>
<img src={logo} alt="App Logo" className="image-logo" />
</div>
);
};
export default App;
If your image isn't displaying, check:
If your image loads slowly, optimize its file size before using it in your app.
Working with images in React is straightforward. Store images in the src
folder and import them into your components, or place them in the public
folder for direct access. You can also style and dynamically load images in React.