ilokesto

목록과 빈 상태

List에는 실제 row와 placeholder가 모두 필요한 경우가 많습니다. Data에는 For, fixed skeleton count에는 Repeat를 사용하세요.

import { For, Repeat, Show } from '@ilokesto/utilinent';

export function OrderList({ orders, loading }) {
  return (
    <Show when={!loading} fallback={<Repeat times={5}>{(i) => <OrderSkeleton key={i} />}</Repeat>}>
      <For each={orders} fallback={<EmptyOrders />}>
        {(order) => <OrderCard key={order.id} order={order} />}
      </For>
    </Show>
  );
}

Tips

  • 실제 data rendering과 placeholder rendering을 분리하세요.
  • Child render function에서 key를 반환하세요.
  • Semantic list markup이 중요하면 For.ul 또는 For.ol을 선호하세요.
  • Repeat는 dynamic array가 아니라 fixed count에만 사용하세요.

큰 목록

For는 array 전체를 map하므로 모든 item이 mount됩니다. 큰 array에서는 한계가 있는 slice를 render하고 명시적으로 늘리세요. Key와 semantic markup은 그대로 제어할 수 있습니다.

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

const PAGE_SIZE = 25;

export function ProductList({ products }: { products: Array<{ id: number; name: string }> }) {
  const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
  const visibleProducts = products.slice(0, visibleCount);
  return (
    <div>
      <For.ul each={visibleProducts}>
        {(product) => <li key={product.id}>{product.name}</li>}
      </For.ul>
      {visibleCount < products.length ? (
        <button type="button" onClick={() => setVisibleCount((count) => count + PAGE_SIZE)}>
          더 보기
        </button>
      ) : null}
    </div>
  );
}
  • Pagination은 mount되는 row 수를 제한할 뿐, state가 보유한 data 크기를 줄이지 않습니다. 25개만 mount해도 500개 항목 array는 여전히 500개를 준비합니다.
  • Slice가 render 사이에 늘어날 수 있다면 array index가 아니라 product.id 같은 안정적인 item identity에 key를 두세요.
  • Virtualized windowing은 별개의 관심사입니다. For, Repeat, Slacker는 virtualization 도구가 아닙니다. 한 번에 수천 개 row를 보여야 한다면 이 pattern을 키우는 대신 전용 virtualization library를 사용하세요. Slice 방식의 특정 memory 절감률은 주장하지 않습니다. 직접 측정하세요.

목차