BrowserBeginnerSSR-safehooks/browser/useViewportSize.ts

useViewportSize

Alias of useWindowSize.

Import

TypeScript
import { useViewportSize } from '@/hooks';

API

Signature

useViewportSize() → { width, height }

When to use

Use useViewportSize instead of re-implementing the same pattern in multiple components.

SSR / Next.js

Returns defaults on the server; hydrates on the client.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useViewportSize
Editor
function VP() {
  const { width, height } = useViewportSize();
  return <p>{width}x{height}</p>;
}
Preview

Implementation source

Full library source for useViewportSize — copy into your project when you are not installing a package yet.

hooks/browser/useViewportSize.ts
import { useWindowSize } from './useWindowSize';

/** Alias of {@link useWindowSize} (viewport = layout viewport). */
export function useViewportSize() {
  return useWindowSize();
}

Copy-paste example

useViewportSize.example.tsx
'use client';

import { useViewportSize } from '@/hooks';

export function VP() {
  const { width, height } = useViewportSize();
  return <p>{width}x{height}</p>;
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: Values are safe during SSR (defaults). Gate layout-dependent UI with useMounted when needed.
  • Source of truth: open hooks/browser/useViewportSize.ts for implementation details