ilokesto

Lazy loading

Use Observer when you already have content but only want to render it when visible. Use Slacker when visibility should start data loading.

<Observer rootMargin="200px" fallback={<ChartPlaceholder />} triggerOnce>
  <HeavyChart />
</Observer>
<Slacker loader={loadChartData} loadingFallback={<ChartPlaceholder />}>
  {(data) => <HeavyChart data={data} />}
</Slacker>

Practical guidance

  • Set rootMargin to preload before the user reaches the content.
  • Use triggerOnce for content that should stay mounted after first visibility.
  • Keep loader functions stable with useCallback when declared inside components.
  • Provide a real fallback height to reduce layout shift.

Lazy loading is not virtualization or cache eviction

Slacker delays the start of loading. Once a load succeeds, the data stays in component state and the observer is triggerOnce, so scrolling the section out of the viewport neither unmounts the content nor frees the data. Do not use Slacker as a cache eviction tool.

Neither Observer nor Slacker windows long lists: For renders every item of the array it receives, so a 10,000-item array mounts 10,000 rows. Lazy loading answers "when does this section start working?"; pagination and virtualization answer "how many rows exist at once?".

Bounding what a lazy list mounts

Cap how many rows are actually mounted with your own pagination state, and let Slacker defer when the list starts loading:

import { useState } from 'react';
import { For, Slacker } from '@ilokesto/utilinent';

const PAGE_SIZE = 25;

function PagedProductList({ items }: { items: Array<{ id: number }> }) {
  const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
  const visibleItems = items.slice(0, visibleCount);
  return (
    <div>
      <For.ul each={visibleItems}>
        {(item) => <li key={item.id}>{item.name}</li>}
      </For.ul>
      {visibleCount < items.length ? (
        <button type="button" onClick={() => setVisibleCount((count) => count + PAGE_SIZE)}>
          Load more
        </button>
      ) : null}
    </div>
  );
}

<Slacker loader={(signal) => fetchProducts(signal)} loadingFallback={<ProductSkeleton />}>
  {(products) => <PagedProductList items={products} />}
</Slacker>

Loading 500 products this way mounts only PAGE_SIZE rows at first and grows by one page per click. Each row keeps a stable key on the item's identity and stays inside semantic <ul> markup. Pagination bounds the mounted DOM; it does not make preparing the data free — fetching, parsing, and holding 500 items still costs what it costs.

For lists with thousands of visible rows at once, pair this pattern with a dedicated virtualization library. Utilinent intentionally ships no virtualization engine, and no specific memory saving is claimed here.

On this page