Skip to main content
<FrontBackGeek/>
frontbackgeek-images

How to import IMAGES and ICONS in react js application ?

FrontBackGeek 2 years ago

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.

© FrontBackGeek.com 2021 All Rights Reserved. DMCA.com Protection Status