next js npm packages tutorials

How To Use react-icons in React Js or Next Js app

Using Icons on a website makes it look so beautiful and easy to navigate, and if we are building one with React Js or any React-based frameworks like Next Js we can use react-icons ( Icons library for react ) to import and use icons from hundreds of icons like Font Awesome 5, Ant Design Icons, Dev Icons, etc.

React Icons uses ES6-based import so we can choose to import only the icons we want to use rather than importing the whole library.

So let’s not waste your time and see how we can use it in our React or Next Js project.

Installing react-icons

To use react-icons we have to install the NPM package of react-icons in our project.

Below is the command to add this package in your application.

npm install react-icons --save

Or, if you are using yarn use the following

yarn add react-icons

Using Icons in our app

As we have installed the react-icons in our application, now we can import and use any of the icons from react-icons.

To see which icons are available you can visit this official website which has the catalog of all the icons available.

For example, I want to use a Hamburger icon from Font Awesome. So for that, there’s an icon named FaBars in react-icons and we can import it like below.

import { FaBars } from 'react-icons/fa';

Can you notice how we import the specific icon from the specific part of the library react-icons, ie. we are just importing FaBars from fa or a font awesome without importing the whole react-icons library, similarly, you can import any icon from the list.

Now we can use it as a component in our app like below

<FaBars />
import "./styles.css";
import { FaBars } from "react-icons/fa";

export default function App() {
  return (
    <div className="App">
      <h1>Hello Coders!</h1>

      <FaBars />
    </div>
  );
}

and you will get this output

output of react-icons 1

image not found

Changing icon’s style

We can also change the style of the icons like size, color, and background color by passing some props.

<FaBars fontSize={40}  />

this will increase the size of the icon and will display this

output of react-icons 2

We can also style icons with CSS by utilizing className

<FaBars fontSize={40} className="my-icon" />
.my-icon{
  color: blue;
  background-color: yellow;
}

this will output

output of react-icons 3

Conclusion

Overall react-icons is a great package to solve all our icon needs in application development, it is also the most popular among React developers.

Finally, this is the full code of this tutorial which you can use as a reference at any time.

Codesandbox Snippet

Well, you seem to be a patient programmer, I wish you never get a bug 😊 Happy coding.

Related Posts

How To Install and Use Flowbite in Next js

Flowbite is an open-source library of UI components based on the utility-first Tailwind CSS framework featuring dark mode support, a Figma design system, templates, and more. It provides…

Using Notistack in Nextjs | React js

Showing toast messages in react apps is a common feature to add, as it’s one of the quickest ways to give feedback to users. There are many toast…

Using React Feather Icons in Next js

Feather Icons is a collection of sleek and beautiful icons that you can use in your projects in different ways and integrate seamlessly with React-based applications. To use…

find unused npm packages

Find and Remove Unused NPM Packages from Your Project

While building apps in an NPM environment it’s easy to accumulate lots of NPM packages in our app that we don’t even need, these package slows down our…

Using Bootstrap Icons in Next js | React

Bootstrap Icons is a Free, high-quality, open-source icon library with over 2,000 icons. You can use them as you like SVGs, SVG sprite, or web fonts. We can…

lucide icons

Using Lucide React Icons in Nextjs | React js

Lucide offers a sleek and versatile icon library that seamlessly integrates with your Next.js or React applications. Let’s explore how to leverage Lucide for beautiful, customizable icons: Lucide…

Leave a Reply

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