Repository navigation
Add useScreenDimensions hook - #59054
Open
ahmedawaad1804 wants to merge 6 commits into
Open
ahmedawaad1804 wants to merge 6 commits into
ahmedawaad1804 wants to merge 6 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: |
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.
Summary:
React Native provides
useWindowDimensionsfor subscribing to application window metrics, but components that need screen metrics must readDimensions.get('screen')and manage their own subscription and cleanup.This PR adds
useScreenDimensions, the screen equivalent ofuseWindowDimensions. It returnswidth,height,scale, andfontScale, and updates subscribed components when any of these values change.The hook uses
useSyncExternalStorewith 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
useScreenDimensionsto 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 --runInBandyarn build-types— regenerated TypeScript declarations and the public API snapshot.yarn test-generated-typescriptyarn test-typescript-legacyyarn flow-check— no errors.git diff --checkTests 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:
useScreenDimensions()anduseWindowDimensions(). Rotate the device and, where supported, fold or unfold it. Confirm the screen hook follows the values reported byDimensions.get('screen').