ilokesto

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

PropDescription
loaderRequired function returning data or a promise.
childrenFunction that receives loaded data.
loadingFallbackUI while the loader is running.
errorFallbackNode or function receiving { isLoading, error, retry }.
threshold, rootMarginPassed to Observer. Defaults are 0.1 and "50px".
maxRetries, retryDelayOptional automatic retry behavior.
onErrorCalled with loader errors.

Guidance

Keep loader stable with useCallback when it is declared inside a component. A changing loader can cause work to restart. Use Slacker for below-the-fold panels, expensive recommendations, or optional UI that should not load until visible.

On this page