Docs

Hooks

Hooks used by the template and conventions for adding application-owned hooks.

The repository currently exports no application-owned custom hooks. Client components use framework hooks directly. This is intentional: a hook should be introduced when stateful logic is reused, not merely to wrap one call.

Hooks currently consumed

HookImport pathPurposeReturn value
usePathnamenext/navigationCurrent URL in navigation componentsstring
useNotebookLayoutfumadocs-ui/layouts/notebookAccess layout slots such as the sidebar triggerNotebook layout context
useFooterItemsfumadocs-ui/utils/use-footer-itemsGet ordered page-tree itemsArray of page items
useTranslations@fuma-translate/reactTranslate pagination labelsTranslation function/object
"use client";

import { usePathname } from "next/navigation";

export function CurrentRoute() {
  const pathname = usePathname();
  return <code>{pathname}</code>;
}

Add a custom hook

Create hooks/use-media-query.ts:

"use client";

import { useEffect, useState } from "react";

export function useMediaQuery(query: string): boolean {
  const [matches, setMatches] = useState(false);

  useEffect(() => {
    const media = window.matchMedia(query);
    const update = () => setMatches(media.matches);
    update();
    media.addEventListener("change", update);
    return () => media.removeEventListener("change", update);
  }, [query]);

  return matches;
}
import { useMediaQuery } from "@/hooks/use-media-query";

const isWide = useMediaQuery("(min-width: 64rem)");

Parameter: query: string, a valid media query. Return value: boolean; it is false during server rendering and until the first effect.

Tip: Prefer CSS media variants for presentation. Use a JavaScript media hook only when behavior or data loading must change.

On this page