lucide-icons / lucide

Beautiful & consistent icon toolkit made by the community. Open-source project and a fork of Feather Icons.
https://lucide.dev
ISC License
11.27k stars 512 forks source link

Using Next.js dynamic import to create a generic icons component can effectively reduce client-side component re-rendering issues. #2081

Open HOJOON07 opened 7 months ago

HOJOON07 commented 7 months ago

Package

Description

In the sidebar and navigation bar components, I was importing each icon component and using icons via the name attribute. However, every time an icon was clicked or an event occurred, all the icon components exhibited a flickering issue.

Use cases

I was able to resolve it by adding React.memo.

import dynamic from 'next/dynamic';
import { memo } from 'react';
import { LucideProps } from 'lucide-react';
import dynamicIconImports from 'lucide-react/dynamicIconImports';

interface IconProps extends LucideProps {
  name: keyof typeof dynamicIconImports;
}

const Icon = memo(({ name, ...props }: IconProps) => {
  const LucideIcon = dynamic(dynamicIconImports[name]);

  return <LucideIcon {...props} />;
});

export default Icon;

Checklist

ishakimanuel commented 1 day ago

Same issue here, also this causing hot reload in client component very laggy.