Skip to content

Add useScreenDimensions hook - #59054

Open
ahmedawaad1804 wants to merge 6 commits into
react:mainfrom
ahmedawaad1804:feat/use-screen-dimensions
Open

ahmedawaad1804 wants to merge 6 commits into
react:mainfrom
ahmedawaad1804:feat/use-screen-dimensions

Conversation

@ahmedawaad1804

@ahmedawaad1804 ahmedawaad1804 commented Oct 14, 2026 •

Copy link
Copy Markdown

Summary:

React Native provides useWindowDimensions for subscribing to application window metrics, but components that need screen metrics must read Dimensions.get('screen') and manage their own subscription and cleanup.

This PR adds useScreenDimensions, the screen equivalent of useWindowDimensions. It returns width, height, scale, and fontScale, and updates subscribed components when any of these values change.

The hook uses useSyncExternalStore with a stable subscription and a cached snapshot. This handles updates between rendering and subscription, preserves snapshot identity when screen metrics are unchanged, and avoids unnecessary renders for window-only changes.

A motivating use case is screen-based drawer sizing on iPhone Duo when the application window and screen have different dimensions.

Includes public JavaScript and Flow exports, TypeScript declarations, the generated public API snapshot, and runtime and type tests.

Previous PR: #58987.

Changelog:

[GENERAL] [ADDED] - Add useScreenDimensions to subscribe to screen width, height, scale, and font scale in React components.

Test Plan:

The following checks passed:

  • yarn test packages/react-native/Libraries/Utilities/__tests__/useScreenDimensions-test.js --runInBand
  • yarn build-types — regenerated TypeScript declarations and the public API snapshot.
  • yarn test-generated-typescript
  • yarn test-typescript-legacy
  • yarn flow-check — no errors.
  • ESLint and Prettier checks on all changed source files.
  • git diff --check

Tests cover initial screen metrics, changes to each metric, unchanged screen metrics during window updates, updates between rendering and subscription, subscription cleanup, subscription stability across updates, and current metrics after remounting.

Manual verification:

  • Render a component displaying both useScreenDimensions() and useWindowDimensions(). Rotate the device and, where supported, fold or unfold it. Confirm the screen hook follows the values reported by Dimensions.get('screen').
  • Verify drawer sizing on iPhone Duo and attach a recording showing the relevant window and screen dimensions.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2026
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_NON_BREAKING

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant