Apply these React optimization techniques to improve component performance and maintainability: 1. **Use stable, unique keys for list items** - Avoid using array indices as keys when rendering lists that can change. Instead, use unique identifiers from your data.
Apply these React optimization techniques to improve component performance and maintainability:
{/* Avoid this */}
{patterns.map((item, index) => (
<div key={`${item.pattern}-${index}`} className="ml-5 flex items-center gap-2">
{/* content */}
</div>
))}
{/* Prefer this */}
{patterns.map((item) => (
<div key={item.pattern} className="ml-5 flex items-center gap-2">
{/* content */}
</div>
))}
// Avoid recreating arrays/objects on every render
const toolGroups = React.useMemo(() => [
// array contents
], [/* dependencies */]);
// Instead of:
const enhanceButton = document.querySelector('[aria-label*="enhance"]');
// Prefer:
const enhanceButtonRef = useRef(null);
// Then pass the ref to the component
<Button ref={enhanceButtonRef} aria-label="enhance" />
// Instead of tracking all instances in a collection:
const [followUpAnswered, setFollowUpAnswered] = useState<Set<number>>(new Set());
// Consider tracking just what's needed:
const [currentFollowUpTs, setCurrentFollowUpTs] = useState<number | null>(null);
Enter the URL of a public GitHub repository