Skip to content

Fix "window is not defined" error in useWindowResize hook #742

@frano-m

Description

@frano-m

Problem

The useWindowResize hook currently assumes that the window object is always available. This causes errors when rendering on the server (such as in SSR or during static builds), where the window object is not defined.

Suggested Solution

  • Update the hook to check if the window object is available before attempting to access its properties (innerWidth/innerHeight), such as in the initial state function and within the resize handler.
  • Return default dimensions if window is not present to avoid runtime errors.
  • Optionally, skip attaching event listeners on the server.

Acceptance Criteria

  • The hook no longer throws a "window is not defined" error in SSR environments.
  • The functionality is preserved for client-side usage.

File: src/hooks/useWindowResize.ts

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions