Repository navigation
Add useScreenDimensions hook - #58908
ahmedawaad1804 wants to merge 2 commits into
Conversation
|
Hi @ahmedawaad1804! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
| export default function useScreenDimensions(): | ||
| DisplayMetrics | DisplayMetricsAndroid { | ||
| const [dimensions, setDimensions] = useState(() => Dimensions.get('screen')); | ||
| useEffect(() => { |
There was a problem hiding this comment.
A quick nit--can you switch to using useSyncExternalStore instead? That should handle the tearing that you had set up in the hook body
Summary:
Add
useScreenDimensions, a screen equivalent ofuseWindowDimensions.Components that need screen metrics currently have to read
Dimensions.get('screen')and manage a change subscription manually. This hook returnswidth,height,scale, andfontScale, updates when those metrics change, and removes its subscription on unmount.One motivating use case is drawer sizing on iPhone Duo, where a component may need screen dimensions rather than the current application window dimensions.
Includes public exports, TypeScript declarations, the generated API snapshot, and tests.
Changelog:
[GENERAL] [ADDED] - Add useScreenDimensions hook for automatically updating screen metrics in React components.
Test Plan:
Passed:
yarn test packages/react-native/Libraries/Utilities/__tests__/useScreenDimensions-test.js --runInBand— all 8 tests passed.yarn 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, ignoring window-only changes, updates between render and subscription, and subscription cleanup on unmount.
Native device behavior, including drawer sizing on iPhone Duo.