#How to import multiple react icons at once using the dynamic method?

5 messages · Page 1 of 1 (latest)

exotic topazBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
crude viper
#
const DynamicFaIcon = dynamic(() =>
    import("react-icons/fa").then(
      (module) => module as unknown as IconType
    )
  )
  const DynamicAiIcon = dynamic(() =>
    import("react-icons/ai").then(
      (module) => module as unknown as IconType
    )
  )

  const [icons, setIcons] = useState<
    (
      | IconType
      | typeof import("react-icons/fa")
      | typeof import("react-icons/ai")
      | null
    )[]
  >([])

  useEffect(() => {
    const iconNames = Object.values(iconMap)

    const loadIcons = async () => {
      const [faModule, aiModule] = await Promise.all([
        DynamicFaIcon,
        DynamicAiIcon,
      ])

      const allIcons = iconNames.map((iconName) => {
        return iconName.startsWith("Fa")
          ? faModule[iconName]
          : iconName.startsWith("Ai")
          ? aiModule[iconName]
          : null
      })
      setIcons(allIcons)
    }
    loadIcons()
  }, [])```
#
const Icon = icons.find((icon) => (icon as { name: string }).name === iconMap[title]) as IconType
return(<div className="relative">
          {Icon ? <Icon /> : "Loading..."}
       </div>)```
#

The main problem now is that the corresponding icon array cannot be obtained in allIcons

#

like this: