Skip to content

usePrefersReducedMotion assumes window exists #3

Description

@gupta-8

Bug

Problem

The hook accesses window.matchMedia inside an effect without checking whether window exists.

Optional chaining does not prevent a crash when window itself is undefined.

Impact

  • Runtime error in SSR environments.
  • Reduced portability and testability.

Location

  • src/utils/usePrefersReducedMotion.ts

Suggested Fix

  • Add a guard at the top of the effect:
    • Exit early if typeof window === "undefined".
  • Then safely use window.matchMedia(...).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions