smolter
API

useRootData

Read the application root loader’s data.

Compatibility

ReactAvailable in this renderer's browser integration.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactAvailable in this renderer's browser integration.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/react @kasperrt/smolter/preact

Signature

function useRootData<T = unknown>(): RouteData<T>;

Parameters

Takes no runtime arguments. Pass the root loader function type to derive its return data, or an explicit data type. Requires RouterApplication or the application mounting helpers; RouterProvider alone does not create root data.

Returns

Returns the current application root value as RouteData<T>. The hook does not fetch or invoke the loader. It updates when the root data store publishes a new value.

Example

Use a type-only import so the server loader implementation stays out of the browser module.

import { useRootData } from "@kasperrt/smolter/preact";
import type { loader } from "../root/loader";

export function AccountName() {
  const root = useRootData<typeof loader>();

  return <span>{root.user.name}</span>;
}

Targeted revalidation

useRouteLoaderData("root") reads the same root value. To refresh it, call the function returned by useRevalidator with { route: "root" }. The application must have registered a root loader callback; naming a target does not create a loader or transport endpoint.

View implementation ↗