question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamic import heroicons react components

See original GitHub issue

Is there any way to make dynamic imports / usage of those react heroicon components?

Normal way: import {ChevronDownIcon} from "@heroicons/react/solid"; … JSX: <ChevronDownIcon/>

Desired: import {${myName}} from "@heroicons/react/solid";

Of course this doesn’t work but i just used it to express my goal. I know i could use a lib like react-svg and then do: <ReactSVG src="/images/icons/myName.svg" />

But then i would need to copy all those SVGs into a target folder but i am already using the react heroicons components elsewhere. Would like to stick only with the heroicons react lib.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

30reactions
IT-MikeScommented, May 31, 2021

This is how I did it, quick but it works for what I needed.

// DynamicHeroIcon.tsx
// Simple Dynamic HeroIcons Component for React (typescript / tsx)
// by: Mike Summerfeldt (IT-MikeS - https://github.com/IT-MikeS)

import { FC } from 'react'
import * as HIcons from '@heroicons/react/outline'

const DynamicHeroIcon: FC<{icon: string}> = (props) => {
  const {...icons} = HIcons
  // @ts-ignore
  const TheIcon: JSX.Element = icons[props.icon]

  return (
    <>
      {/* @ts-ignore */}
      <TheIcon className="h-6 w-6 text-white" aria-hidden="true" />
    </>
  )
}

export default DynamicHeroIcon

Usage:

<DynamicHeroIcon icon={'CogIcon'} />
28reactions
uvisgrinfeldscommented, Jun 26, 2021

I needed dynamic import for Vue. Perhaps someone finds this useful:

<template>
  <component v-if="isLoaded" :is="heroIcons[name]" />
</template>

<script>
import * as heroIcons from "@heroicons/vue/solid";

export default {
  data() {
    return {
      isLoaded: false,
      heroIcons: heroIcons
    };
  },
  props: {
    name: String,
  },
  mounted() {
    this.isLoaded = true;
  },
};
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Heroicons import with Next.js - Stack Overflow
I'm using heroicons in my Next.js project and since it currently doesn't support dynamic imports (by passing icon name to component) I ...
Read more >
[Solved]-Dynamic Heroicons import with Next.js-Reactjs
Can i import the SVG file as a react component with dynamic import() Expressions? Conditionally import module using next js dynamic import, SSR...
Read more >
A utility library to lookup TailwindCSS Heroicons by their name
If you need to import an icon dynamically based on it's name, you can with this library. By using the function below, you...
Read more >
React dynamic component from array - Stack Overflow
I have an list of menu: import { HomeIcon } from '@heroicons/react/outline' export const menus = [ { name: 'Home', to: '/', desc:...
Read more >
React Dynamic Imports and How To Use Them - Common Ninja
In this article, we are going to discuss dynamic imports, how to dynamically render components in React, and when to use dynamic imports....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found