How to import IMAGES and ICONS in react js application ?

For every web application, image and icons are major part of application. In react js importing images and icons are little different compared to other libraries and frameworks.

Let’s understand how we can add images and icons inside a react js project.

Adding Images :

Inside App.js component, we are importing a image file or a react js logo file, location is this file should be in SRC folder.

Inside this SRC folder you can create another folders like assets and images but for now we are just placing this logo image file inside SRC folder and importing it into our App component.

import ‘./App.css’;
import logo from ‘./logo.png’;
function App() {
 
  return (
    <div className=“App”>
     <img src={logo}/>
    </div>
  );
}
export default App;

Variable name for importing image can be anything, like here logo is the name for image but you can write any name to refer this image.

Inside image tag we are assigning this variable to src attribute.

Output for this code will be :

Adding Icons :

To imports icons in react js project we need to install the react icons first and for that we need to run this command.

npm install reacticonssave

After installing this, now you can add any icon from react icons library.

Click on this link to see all the react icons list.

https://react-icons.github.io/react-icons

To add a icon inside our project we need to import the icon library for example.

import ‘./App.css’;
import { FaBeer } from ‘react-icons/fa’;
import { BiArchive } from ‘react-icons/bi’;
import { BsFillAlarmFill } from ‘react-icons/bs’;
function App() {
 
  return (
    <div className=“App”>
     <FaBeer className=‘fontSize-200’/>
     <BiArchive className=‘fontSize-200’ />
     <BsFillAlarmFill className=‘fontSize-200’ />
    </div>
  );
}
export default App;

Inside App.css file add font-Size-200 class and set font-size:200px;

.fontSize-200{
  font-size: 200px;
}

Output for above code is :

So the basic code structure is

import {ICON_NAME} from ‘react-icons/ICONS_LIBRARY_NAME’
 

and to call icons inside the JSX

<ICON_NAME></ICON_NAME>

You can find hundreds of icons on react icons page.

If you have any question feel free to comment.

Related Posts

angular vs react-min

What is the difference between Angular & React JS, Which one you should choose?

Angular & React JS both are use for Frontend Development, To make Single page applications or Web Apps. Here is a quick difference between both of them….

react js icon

What is Redux? What is action, reducer and store in Redux

In this blog we will discus about the redux, what is Redux and how you can use it in React application. What is Redux? Redux is a…

react js icon

What is useEffect Hook? How to Fetch data from API using useEffect

useEffect hook is used to manage side effects that are not related to component rendering. Mostly useEffect hook is used for managing console logs, animations, loading data,…

react js icon

What is useReducer Hook? What is the difference between useState and useReducer Hooks?

useReudcer hook takes the current state and returns a new state, it is used for state management. With useReducer, we can directly call the function inside the…

react js icon

Creating React JS App without using npx create-react-app command

It’s easy to create a react application by just running npx create-react-app project_name command, do you know what are the basic requirements for creating a react application…

react js icon

What is the useState() hooks in React JS | How to use useState() hook in React JS

Hooks are very important in react js and the most common hook is the useState Hook. In this blog, we will look into the functionality of this…

Leave a Reply

Your email address will not be published. Required fields are marked *