Skip to content

Commit e8f86fc

Browse files
authored
fix(lucide-react): ignore stale DynamicIcon loads after the name changes (#5060)
When the name prop changes while the previous icon is still loading, the earlier import could resolve last and overwrite the newer icon, leaving DynamicIcon showing the wrong icon. The effect now ignores results from a superseded name.
1 parent 03f11e2 commit e8f86fc

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎packages/lucide-react/src/DynamicIcon.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,21 @@ const DynamicIcon = forwardRef<SVGSVGElement, DynamicIconComponentProps>(
5555
const [iconData, setIconData] = useState<LucideIconData>();
5656

5757
useEffect(() => {
58+
let ignore = false;
59+
5860
getIconData(name)
59-
.then(setIconData)
61+
.then((data) => {
62+
if (!ignore) {
63+
setIconData(data);
64+
}
65+
})
6066
.catch((error) => {
6167
console.error(error);
6268
});
69+
70+
return () => {
71+
ignore = true;
72+
};
6373
}, [name]);
6474

6575
if (iconData == null) {

‎packages/lucide-react/tests/DynamicIcon.spec.tsx‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1-
import { describe, it, expect } from 'vitest';
1+
import { describe, it, expect, vi } from 'vitest';
22
import { act, render, waitFor, type RenderResult } from '@testing-library/react';
33

44
import DynamicIcon from '../src/DynamicIcon';
55

6+
const { delayedImports, pendingImports } = vi.hoisted(() => ({
7+
delayedImports: new Map<string, Promise<void>>(),
8+
pendingImports: new Map<string, Promise<unknown>>(),
9+
}));
10+
11+
vi.mock('../src/dynamicIconImports', async (importOriginal) => {
12+
const { default: dynamicIconImports } =
13+
await importOriginal<typeof import('../src/dynamicIconImports')>();
14+
15+
return {
16+
default: Object.fromEntries(
17+
Object.entries(dynamicIconImports).map(([name, importIcon]) => [
18+
name,
19+
() => {
20+
const pendingImport = (delayedImports.get(name) ?? Promise.resolve()).then(importIcon);
21+
pendingImports.set(name, pendingImport);
22+
23+
return pendingImport;
24+
},
25+
]),
26+
),
27+
};
28+
});
29+
630
describe('Using DynamicIcon Component', () => {
731
it('should render icon by given name', async () => {
832
let container: RenderResult['container'];
@@ -81,4 +105,30 @@ describe('Using DynamicIcon Component', () => {
81105
expect(ref.current).toBeInstanceOf(SVGSVGElement);
82106
});
83107
});
108+
109+
it('should render the latest icon when a previous icon finishes loading later', async () => {
110+
let resolveCameraImport = () => {};
111+
delayedImports.set(
112+
'camera',
113+
new Promise((resolve) => {
114+
resolveCameraImport = resolve;
115+
}),
116+
);
117+
118+
const { container, rerender } = render(<DynamicIcon name="camera" />);
119+
rerender(<DynamicIcon name="smile" />);
120+
121+
await waitFor(() => {
122+
expect(container.firstChild).toHaveClass('lucide-smile');
123+
});
124+
125+
await act(async () => {
126+
resolveCameraImport();
127+
await pendingImports.get('camera');
128+
await new Promise((resolve) => setTimeout(resolve));
129+
});
130+
131+
expect(container.firstChild).toHaveClass('lucide-smile');
132+
expect(container.firstChild).not.toHaveClass('lucide-camera');
133+
});
84134
});

0 commit comments

Comments
 (0)