Repository navigation
Add useScreenDimensions hook - #59160
Open
ahmedawaad1804 wants to merge 7 commits into
Open
ahmedawaad1804 wants to merge 7 commits into
ahmedawaad1804 wants to merge 7 commits into
Conversation
… improved performance and subscription management. Update tests to verify subscription behavior across screen updates and ensure correct metric reading when unmounted.
…awaad1804/react-native into feat/use-screen-dimensions
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
Contributor
|
@ahmedawaad1804 could you refrain from closing and reopening new copies of this PR? This is fine as is |
Author
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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.