비동기 데이터 가이드
Async UI에는 보통 loading, error, empty, success 네 가지 상태가 있습니다. Utilinent를 쓰면 깊은 ternary 없이 각 상태를 눈에 보이게 유지할 수 있습니다.
import { For, Show } from '@ilokesto/utilinent';
export function Products({ data, loading, error }) {
return (
<Show when={!loading} fallback={<ProductsSkeleton />}>
<Show when={!error} fallback={<ErrorPanel error={error} />}>
<For.ul each={data?.items} fallback={<EmptyProducts />}>
{(product) => <ProductRow key={product.id} product={product} />}
</For.ul>
</Show>
</Show>
);
}추천 순서
- Loading을 먼저 gate합니다.
- Error를 두 번째로 gate합니다.
- Array는
For와 empty fallback으로 렌더링합니다. - Row rendering은 작은 component로 분리합니다.
이 순서는 사용자가 보는 흐름과 같습니다. 먼저 기다리고, error를 복구하고, empty 또는 populated data를 처리합니다.
Mount 또는 Slacker를 쓰는 경우
Component 자체가 node-producing function을 resolve해야 한다면 Mount를 사용하세요. Section이 visible해질 때까지 loading을 시작하지 않아야 한다면 Slacker를 사용하세요.
Function identity가 비동기 작업의 재시작을 결정합니다
Mount와 Slacker 모두 비동기 작업의 기준을 function이 capture한 값이 아니라 function identity로 삼습니다. Inline () => fetchX(id)나 inline onError는 부모가 render할 때마다 새 function이 되므로 관련 없는 re-render가 작업을 다시 시작합니다. 실제 입력을 useCallback dependency에 넣으세요. useCallback(() => fetchX(id), [id])는 관련 없는 re-render를 견디고, id가 바뀌는 것이 의도된 재시작입니다. onError의 identity 변경도 Mount의 현재 attempt를 다시 시작합니다.
두 component 모두 현재 attempt의 AbortSignal을 factory와 loader에 전달하므로, 교체되거나 unmount된 attempt는 underlying request를 취소할 수 있습니다. 검증된 예제와 함께 전체 취소·identity 계약은 Mount 레퍼런스와 Slacker 레퍼런스를 보세요.