fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← React

React / 6 MIN READ

Step 1: Install Lucide React

Sure! Lucide React is a React component library for Lucide icons, which is an open-source icon library. It's super handy for adding icons to your React pro

From the original Fervor library. Examples may use older package versions.

Sure! Lucide React is a React component library for Lucide icons, which is an open-source icon library. It’s super handy for adding icons to your React projects. Let’s go through how to install and use Lucide React step-by-step.

Step 1: Install Lucide React

First, you need to install lucide-react in your React project. Open your terminal and run the following command:

npm install lucide-react

Or, if you prefer using Yarn:

yarn add lucide-react

Step 2: Import and Use Icons

Once installed, you can start using Lucide icons in your components. Here’s how you can do it:

  1. Import the icon(s) you want to use.
  2. Render the icon in your component.

Let’s go through a simple example.

Example: Adding an Icon to Your Component

  1. Create a new component (or open an existing one) where you want to use the icon. For this example, let’s create a new component called IconExample.js.

  2. Import the icon you want to use from lucide-react. For instance, let’s use the Camera icon.

// src/IconExample.js
import React from 'react';
import { Camera } from 'lucide-react';

const IconExample = () => {
  return (
    <div className="icon-example">
      <h1>Hello, Lucide React!</h1>
      <Camera color="blue" size={48} />
    </div>
  );
}

export default IconExample;

Step 3: Customize the Icon

You can easily customize the icon by passing props to it. Lucide React icons accept several props, including size, color, strokeWidth, and more.

  • size: Adjusts the size of the icon.
  • color: Changes the color of the icon.
  • strokeWidth: Modifies the stroke width of the icon.

Here’s an example with customized properties:

// src/IconExample.js
import React from 'react';
import { Camera } from 'lucide-react';

const IconExample = () => {
  return (
    <div className="icon-example">
      <h1>Hello, Lucide React!</h1>
      <Camera color="red" size={64} strokeWidth={1} />
    </div>
  );
}

export default IconExample;

Step 4: Use Your Component

Now, you can use the IconExample component in your app. Open your App.js and import the IconExample component.

// src/App.js
import React from 'react';
import IconExample from './IconExample';

function App() {
  return (
    <div className="App">
      <IconExample />
    </div>
  );
}

export default App;

Step 5: Run Your Application

Start your React application to see the icon in action:

npm start

Or, if you’re using Yarn:

yarn start

Open http://localhost:3000 in your browser, and you should see your icon rendered on the screen.

Recap

  1. Install Lucide React: npm install lucide-react or yarn add lucide-react.
  2. Import Icons: Import the specific icons you need from lucide-react.
  3. Render Icons: Use the icons in your components and customize them with props.
  4. Use Your Component: Integrate your icon-containing component into your application.

Additional Tips

  • Explore More Icons: Lucide has a wide range of icons. You can check out all available icons in their icon gallery.
  • Create Custom Icons: If you need a custom icon, you can create one and integrate it similarly.
  • Styling: Use Tailwind CSS (or any other styling method) to style your icons and components further.

That’s it! You now know how to install and use Lucide React icons in your React project. If you have any questions or run into any issues, feel free to ask. Happy coding!

BONUS COOL STUFF

Lucide React offers a wide range of icons that can be customized and used in various creative ways. Here are some cool things you can do with Lucide React:

1. Dynamic Icon Colors

You can change icon colors dynamically based on state or props. For example, changing the icon color when a button is clicked:

import React, { useState } from 'react';
import { Heart } from 'lucide-react';

const DynamicColorIcon = () => {
  const [liked, setLiked] = useState(false);

  return (
    <div>
      <Heart 
        color={liked ? 'red' : 'grey'} 
        size={48} 
        onClick={() => setLiked(!liked)} 
        style={{ cursor: 'pointer' }}
      />
    </div>
  );
}

export default DynamicColorIcon;

2. Animated Icons

You can use CSS animations to create effects on your icons. For instance, a rotating loading spinner:

import React from 'react';
import { Loader } from 'lucide-react';
import './App.css'; // Make sure to create and import your CSS file

const SpinnerIcon = () => {
  return (
    <div className="spinner">
      <Loader size={48} />
    </div>
  );
}

export default SpinnerIcon;
/* App.css */
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.spinner {
  display: inline-block;
  animation: spin 1s linear infinite;
}

3. Icon Buttons

Create icon buttons that perform actions or navigate to different parts of your app:

import React from 'react';
import { Edit, Trash } from 'lucide-react';

const IconButton = ({ icon: Icon, onClick }) => (
  <button onClick={onClick} className="p-2 bg-gray-200 rounded-full hover:bg-gray-300">
    <Icon size={24} />
  </button>
);

const IconButtonExample = () => {
  return (
    <div className="flex space-x-4">
      <IconButton icon={Edit} onClick={() => alert('Edit clicked!')} />
      <IconButton icon={Trash} onClick={() => alert('Delete clicked!')} />
    </div>
  );
}

export default IconButtonExample;

4. Conditional Icons

Display different icons based on a condition, like user status or application state:

import React from 'react';
import { CheckCircle, XCircle } from 'lucide-react';

const StatusIcon = ({ status }) => {
  return (
    <div>
      {status === 'success' ? <CheckCircle color="green" size={48} /> : <XCircle color="red" size={48} />}
    </div>
  );
}

const ConditionalIconsExample = () => {
  return (
    <div>
      <StatusIcon status="success" />
      <StatusIcon status="error" />
    </div>
  );
}

export default ConditionalIconsExample;

5. Themed Icons

Change the style of icons based on the current theme (light or dark mode):

import React from 'react';
import { Sun, Moon } from 'lucide-react';

const ThemedIcon = ({ theme }) => {
  return (
    <div>
      {theme === 'light' ? <Sun color="yellow" size={48} /> : <Moon color="white" size={48} />}
    </div>
  );
}

const ThemedIconsExample = () => {
  const [theme, setTheme] = React.useState('light');

  return (
    <div className={theme === 'light' ? 'bg-white' : 'bg-black'} style={{ height: '100vh' }}>
      <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
        Toggle Theme
      </button>
      <ThemedIcon theme={theme} />
    </div>
  );
}

export default ThemedIconsExample;

6. Icon Overlays

Overlay icons on top of images or other elements to indicate actions:

import React from 'react';
import { Camera } from 'lucide-react';

const IconOverlay = () => {
  return (
    <div className="relative">
      <img src="your-image.jpg" alt="Example" className="w-full h-auto" />
      <Camera size={48} className="absolute bottom-4 right-4 text-white" />
    </div>
  );
}

export default IconOverlay;

7. Interactive Icons

Create interactive icons that respond to user input:

import React, { useState } from 'react';
import { Volume2, VolumeX } from 'lucide-react';

const InteractiveIcon = () => {
  const [muted, setMuted] = useState(false);

  return (
    <div onClick={() => setMuted(!muted)} className="cursor-pointer">
      {muted ? <VolumeX size={48} /> : <Volume2 size={48} />}
    </div>
  );
}

export default InteractiveIcon;

8. Icon Lists

Display a list of icons, useful for features or tech stacks:

import React from 'react';
import { Code, Monitor, Smartphone } from 'lucide-react';

const IconList = () => {
  return (
    <ul className="flex space-x-4">
      <li>
        <Code size={48} />
        <p>Development</p>
      </li>
      <li>
        <Monitor size={48} />
        <p>Design</p>
      </li>
      <li>
        <Smartphone size={48} />
        <p>Mobile</p>
      </li>
    </ul>
  );
}

export default IconList;

Recap of Cool Uses

  1. Dynamic Icon Colors: Change colors based on state or props.
  2. Animated Icons: Use CSS animations to add effects.
  3. Icon Buttons: Create interactive buttons with icons.
  4. Conditional Icons: Display different icons based on conditions.
  5. Themed Icons: Change icon styles based on themes.
  6. Icon Overlays: Overlay icons on images or other elements.
  7. Interactive Icons: Icons that respond to user input.
  8. Icon Lists: Display lists of icons for various features.

Lucide React provides a flexible and powerful way to incorporate icons into your React applications, allowing for a wide range of creative and interactive uses. Happy coding!!

Keep your curiosity going.Explore more React →
287 TUTORIALS · 22 TOPICSREADY