Blog>
Snippets

Memoization with React Components

Showcases the use of memoization in React components within TanStack Virtual Grid to prevent unnecessary re-renders and improve performance.
import React, { useMemo } from 'react';
Import necessary hooks from React for memoization.
const MemoizedItem = React.memo(function Item({ data }) {
  // Component content here, e.g., a row in a virtual grid
  return <div>{data}</div>;
});
Defines a memoized component to prevent re-renders unless props change.
function VirtualizedGrid({ items }) {
  const renderRow = useMemo(() => (
    ({ index, style }) => <MemoizedItem style={style} data={items[index]} />
  ), [items]);
  // Implementation details on using the TanStack Virtual Grid go here
  return <div>Your grid component using TanStack Virtual</div>;
}
Implements a virtualized grid with TanStack Virtual, using memoized rows to improve performance.