smolter
API

useURLLocation

Read the visible URL together with navigation state and its history key.

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 useURLLocation(): URL & { state: unknown; key: string };

Parameters

Takes no arguments and requires RouterProvider. Browser history state is maintained by the application navigation integration.

Returns

Returns a memoized URL with additional state and key properties. Standard URL properties such as pathname, search, hash, origin, and href remain available. The key identifies the current application history entry.

Example

Use this value for display and derived state. Navigate through Link, useNavigate, or the router rather than mutating this URL object.

import { useURLLocation } from "@kasperrt/smolter/preact";

export function CurrentAddress() {
  const location = useURLLocation();

  return <code>{location.pathname}{location.search}</code>;
}

Visible versus pending

With retained views, this URL follows the committed snapshot while the next route loads. useNavigation().location reports a pending destination when the integration exposes one. A bare router does not independently maintain navigation history keys and state.

View implementation ↗