Hooks

useIsMounted

Guards async completions and timers so setState does not run after unmount.

Import

import { useIsMounted } from "@zentauri-ui/zentauri-components/lib/useIsMounted";

Demo source

Full client component used for this preview (same JSX as below). Adjust paths if you copy outside this app.

"use client";

import { useIsMounted } from "@zentauri-ui/zentauri-components/lib/useIsMounted";
import { Button } from "@zentauri-ui/zentauri-components/ui/buttons";
import { HookDemoPanel } from "@/components/preview/hooks/hook-demo-panel";
import { useEffect, useRef, useState } from "react";

export default function HookDemo() {
  const isMounted = useIsMounted();
  const [label, setLabel] = useState("");
  const timeoutRef = useRef<number | undefined>(undefined);

  useEffect(() => {
    return () => {
      window.clearTimeout(timeoutRef.current);
    };
  }, []);

  const run = () => {
    window.clearTimeout(timeoutRef.current);
    timeoutRef.current = window.setTimeout(() => {
      timeoutRef.current = undefined;
      if (!isMounted()) {
        return;
      }
      setLabel("mounted (still on this page after 50ms)");
    }, 50);
  };

  return (
    <HookDemoPanel title="Interactive demo">
      <p className="mb-4 text-sm text-slate-400">
        Schedules a timeout, then updates state only if this demo is still mounted. Leave
        this page before 50ms and the callback does nothing (and the timer is cleared on
        unmount).
      </p>
      <Button type="button" onClick={run}>
        Check mounted in 50ms
      </Button>
      {label ? (
        <p className="mt-4 text-sm text-white">
          Result: <span className="text-cyan-300">{label}</span>
        </p>
      ) : null}
    </HookDemoPanel>
  );
}

Interactive demo

Schedules a timeout, then updates state only if this demo is still mounted. Leave this page before 50ms and the callback does nothing (and the timer is cleared on unmount).

What it does

useIsMounted ships as a small client module you can import from @zentauri-ui/zentauri-components/lib/useIsMounted. Use it when the behavior matches your integration without copying utility code.

API notes

Read the interactive demo on this page for the parameters used in typical flows. Refer to TypeScript types exported next to the hook for exhaustive options.

Common use cases

  • Reuse the same hook in client components across Next.js App Router routes.
  • Keep server components free of browser APIs; colocate interactive demos in client files.
  • Compose hooks with Zentauri UI primitives for overlays, forms, and data views.
  • Align documentation with published @zentauri-ui/zentauri-components/lib/* paths.

Accessibility

Hooks manipulate behavior, not roles. Pair them with appropriate elements, ARIA attributes, and keyboard handlers so assistive technologies receive a coherent experience.

Next.js integration notes

Keep interactive subtrees in client components. When you only need static copy, leave it in the server layout and pass children into a client boundary that calls the hook.

FAQ

Can I import this hook from a server component?

No. These hooks rely on browser APIs or React client lifecycle. Import them from a file marked with the "use client" directive or from a client child component.

Does the preview site bundle the same code as npm?

The component library app depends on the workspace package. Published builds resolve the same export paths under @zentauri-ui/zentauri-components.

Where should I validate accessibility for hook-driven UI?

Hooks do not replace semantics. Test focus order, labels, and keyboard flows in your real layout with assistive technologies you support.