Slacker
Slacker combines Observer with a loader. It waits until the element enters the viewport, runs the loader, and renders children(loadedData) when data is available.
import { Slacker } from '@ilokesto/utilinent';
<Slacker
loader={() => fetch('/api/recommendations').then((res) => res.json())}
loadingFallback={<p>Loading recommendations...</p>}
errorFallback={({ error, retry }) => (
<button onClick={retry}>Retry: {error?.message}</button>
)}
>
{(items) => <RecommendationList items={items} />}
</Slacker>Props
| Prop | Description |
|---|---|
loader | Required function invoked with the current attempt's AbortSignal; returns data or a promise. May ignore the signal. |
children | Function that receives loaded data. |
loadingFallback | UI while the loader is running. |
errorFallback | Node or function receiving { isLoading, error, retry }. |
threshold, rootMargin | Passed to Observer. Defaults are 0.1 and "50px". |
maxRetries, retryDelay | Optional automatic retry behavior. |
onError | Called with loader errors. |
Retry lifecycle
The load triggered by the first intersection is attempt one. maxRetries is the number of additional attempts available after that initial attempt, and retryDelay is applied before each automatic retry. The retry function from errorFallback uses the same retry budget as automatic retries, starts immediately, and cancels any automatic retry that is waiting.
Only one attempt can run at a time, so repeated retry clicks while a request is in flight are coalesced. Each failed active attempt calls onError once. A successful load, a new loader reference, or unmounting cancels scheduled retries and ignores stale loader results.
Cancellation with AbortSignal
Every attempt owns a fresh controller — the initial load, each automatic or manual retry, and each restart after a loader replacement — and the loader receives that attempt's signal.
<Slacker
loader={(signal) =>
fetch(`/api/recommendations?cursor=${cursor}`, { signal }).then((res) => res.json())
}
loadingFallback={<p>Loading recommendations...</p>}
>
{(items) => <RecommendationList items={items} />}
</Slacker>The signal aborts when the component unmounts, when the loader reference is replaced, and when a newer attempt supersedes a failed one. A settled attempt keeps its signal inert: a successful load does not abort its signal.
- Zero-argument loaders keep working unchanged: JavaScript ignores the extra argument, and the stale-result guards still ignore late settlements.
- A loader that never reads the signal is not cancelled; it only stops affecting the UI.
- An
AbortErrorrejection from an attemptSlackeralready aborted is dropped silently: noonError, no retry, no console error, and no retry timer is left scheduled. - An
AbortErrorthat surfaces from the live attempt is treated like any other failure and consumes the retry budget.
Loader identity and re-runs
Slacker restarts the loader when its reference changes — not when values captured in the closure change. An inline loader is a new reference on every parent render, so every unrelated re-render invalidates the current generation and starts loading again:
export function Recommendations({ cursor }: { cursor: string }) {
const [ticks, setTicks] = useState(0);
return (
<div>
{/* New reference every render: each parent re-render restarts loading. */}
<Slacker
loader={() => fetch(`/api/recommendations?cursor=${cursor}`).then((res) => res.json())}
loadingFallback={<RecommendationsSkeleton />}
>
{(items) => <RecommendationList items={items} />}
</Slacker>
<button type="button" onClick={() => setTicks((count) => count + 1)}>
unrelated state {ticks}
</button>
</div>
);
}Put the real inputs in the dependency list instead of memoizing everything blindly. A stable loader survives unrelated re-renders; a changed input is the intended way to reload:
export function Recommendations({ cursor }: { cursor: string }) {
const [ticks, setTicks] = useState(0);
const loadRecommendations = useCallback(
(signal: AbortSignal) =>
fetch(`/api/recommendations?cursor=${cursor}`, { signal }).then((res) => res.json()),
[cursor],
);
return (
<div>
<Slacker loader={loadRecommendations} loadingFallback={<RecommendationsSkeleton />}>
{(items) => <RecommendationList items={items} />}
</Slacker>
<button type="button" onClick={() => setTicks((count) => count + 1)}>
unrelated state {ticks}
</button>
</div>
);
}In this version clicking the button does not reload, while changing cursor starts a new generation and aborts the previous attempt's signal. A reference change is the deliberate update channel: create a new loader when the inputs actually changed, keep the same one when they did not.
Guidance
Use Slacker for below-the-fold panels, expensive recommendations, or optional UI that should not load until visible. Slacker defers the start of loading; it does not unmount content or reclaim data after the section leaves the viewport. See the lazy loading guide for the boundary between lazy loading and virtualization.