ilokesto

Mount

Mount renders a direct React node immediately. To perform asynchronous work, pass a factory that returns a Promise. Direct Promise and PromiseLike children are unsupported with both React 18 and React 19 types.

import { Mount } from '@ilokesto/utilinent';

<Mount fallback={<p>Preparing preview...</p>}>
  {async () => <Preview data={await loadPreview()} />}
</Mount>

Props

PropTypeDescription
childrenMountNode | ((signal: AbortSignal) => ReactNode | Promise<ReactNode>)Direct node or a synchronous/asynchronous node factory. MountNode is ReactNode without PromiseLike values. The factory may ignore the signal; zero-argument factories are accepted unchanged.
fallbackReactNodeInitial output for a factory, retained while its Promise is pending and after an error. Defaults to null.
onError(error: unknown) => voidReceives the original error when the current factory throws or rejects.

Factory-only async work

Call the asynchronous operation inside the child factory:

<Mount fallback={<Spinner />} onError={reportError}>
  {() => loadPreview().then((preview) => <Preview data={preview} />)}
</Mount>

Do not start the operation during render and pass its Promise as children. Direct Promise<ReactElement>, Promise<string>, and other PromiseLike children are intentionally unsupported by both the public type and runtime contract, even though React 19 includes promises in ReactNode.

Timing, fallback, and errors

  • A direct node is the initial output and does not use fallback.
  • A function child starts with fallback. Mount invokes the function in a layout effect after commit.
  • A synchronous result replaces the fallback in that effect, normally before the browser paints.
  • A Promise result keeps the fallback visible until fulfillment.
  • A synchronous throw or active Promise rejection keeps the fallback visible, writes the existing console error, and calls onError once for that failed invocation.
  • React Strict Mode can replay effects in development, so factories must be safe to invoke more than once.

Tag form

<Mount.div fallback={<Spinner />} className="preview">
  {() => <Preview />}
</Mount.div>

Race safety

Changing children starts a newer call. Fulfillment or rejection from an older call is ignored: it cannot replace the current output and does not call onError. Settlements after unmount are ignored in the same way. Replacing pending async work with a direct node displays that node immediately.

Cancellation with AbortSignal

The factory is invoked with the current attempt's AbortSignal. Mount aborts that signal when the attempt is superseded: when children or onError identity changes, when the component unmounts, or when StrictMode discards the first effect pass in development.

<Mount fallback={<Spinner />} onError={reportError}>
  {(signal) =>
    fetch(`/api/preview/${id}`, { signal })
      .then((res) => res.json())
      .then((preview) => <Preview data={preview} />)
  }
</Mount>
  • Zero-argument factories keep working unchanged: JavaScript ignores the extra argument, and the stale-result guards above still ignore late settlements.
  • Work that never reads the signal is not cancelled by Mount; it only stops affecting the UI.
  • An AbortError rejection from an attempt Mount already aborted is dropped silently: no onError, no console error, no extra work.
  • An AbortError that surfaces from the current, live attempt is reported like any other failure.

Function identity and re-runs

Mount restarts the factory when the children or onError function identity changes — not when values captured in the closure change. An inline factory or inline onError is a new identity on every parent render, so every unrelated re-render starts the work again:

export function PreviewPanel({ id }: { id: string }) {
  const [ticks, setTicks] = useState(0);
  return (
    <div>
      {/* New function identity every render: each parent re-render restarts the request. */}
      <Mount fallback={<Spinner />}>
        {() => fetchPreview(id).then((preview) => <Preview data={preview} />)}
      </Mount>
      <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 factory survives unrelated re-renders, and a changed input is the intended way to start a newer request:

export function PreviewPanel({ id }: { id: string }) {
  const [ticks, setTicks] = useState(0);
  const loadPreview = useCallback(
    () => fetchPreview(id).then((preview) => <Preview data={preview} />),
    [id],
  );
  return (
    <div>
      <Mount fallback={<Spinner />} onError={reportError}>{loadPreview}</Mount>
      <button type="button" onClick={() => setTicks((count) => count + 1)}>
        unrelated state {ticks}
      </button>
    </div>
  );
}

In this version clicking the button does not re-run loadPreview, while changing id starts a new attempt and aborts the previous signal. A reference change is the deliberate update channel: create a new function when the inputs actually changed, keep the same one when they did not. Changing onError identity also restarts the current attempt, so keep it stable the same way when it is passed inline.

On this page