Skip to content

Instantly share code, notes, and snippets.

@Evavic44
Last active July 11, 2023 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Evavic44/3acc81896d145fc717dd101e5adb3d72 to your computer and use it in GitHub Desktop.
Save Evavic44/3acc81896d145fc717dd101e5adb3d72 to your computer and use it in GitHub Desktop.
Snippet on how to dynamically import icons based on a string in React

First install the iconify library or follow this guide

npm install --save-dev @iconify/react

// using yarn
yarn add --dev @iconify/react

Next import the component from the iconify library

import { Icon } from '@iconify/react';

Display the icon by passing in the icon name into the icon parameter

Let's say you have an array of icons, using template literals, you can define these icons without having to import them individually.

Social icons coming from Boxicons Logo

const social = [github, linkedin, twitter, youtube];

function Social() {
  return(
    <div>
      {social.map((string) => (
        <Icon icon={`bxl:${string}`} width={30} height={30} aria-hidden="true" />
      ))}
    </div>
  );
}

export default Social
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment