Skip to content

useWhenMounted

Execute a callback and expose its return value as a reactive Observable<T | undefined> — only after the component has mounted. Returns undefined during SSR and before hydration, then re-evaluates with the actual callback value once mounted. Unlike useSupported, the return value is not coerced to boolean — the full type T is preserved.

import { useWhenMounted } from "@usels/web";
const windowWidth = useWhenMounted(() => window.innerWidth);
// undefined on the server, actual width after mount
console.log(windowWidth.get());
import { useWhenMounted } from "@usels/web";
const scrollY = useWhenMounted(() => window.scrollY);
export type UseWhenMountedReturn<T> = Observable<T | undefined>;
export declare function useWhenMounted<T>(callback: () => T): UseWhenMountedReturn<T>;

View on GitHub

  • tigerwest
  • a7392ab 2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)