Skip to content

Dimensions API does not handles orientation change #50861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
roman-zaitsev opened this issue Apr 23, 2025 · 4 comments
Open

Dimensions API does not handles orientation change #50861

roman-zaitsev opened this issue Apr 23, 2025 · 4 comments
Labels
API: Dimensions Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.

Comments

@roman-zaitsev
Copy link

Description

Dimensions API always return portrait mode values (height > width) even if app was started in landscape mode.
Try to rotate device and check console output - height > width always.

Steps to reproduce

  1. npx @react-native-community/cli@latest init rn791

  2. cd rn791

  3. yarn

  4. cd ios && pod install

  5. add next lines of code to App.tsx
    `useEffect(() => {
    const updateOrientation = () => {
    const {height, width} = Dimensions.get('window');
    const orientation = width > height ? 'landscape' : 'portrait';
    console.log('new orientation: ', orientation);
    };

    updateOrientation();
    const subscription = Dimensions.addEventListener(
    'change',
    updateOrientation,
    );

    return () => {
    subscription?.remove();
    };
    }, []);`

React Native Version

0.79.1

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info

System:
  OS: macOS 15.4.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 104.02 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.12.0
    path: ~/.nvm/versions/node/v22.12.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v22.12.0/bin/yarn
  npm:
    version: 10.9.0
    path: ~/.nvm/versions/node/v22.12.0/bin/npm
  Watchman:
    version: 2024.11.25.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.4
      - iOS 18.4
      - macOS 15.4
      - tvOS 18.4
      - visionOS 2.4
      - watchOS 11.4
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.3 AI-243.24978.46.2431.13208083
  Xcode:
    version: 16.3/16E140
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.13
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 18.0.0
    wanted: 18.0.0
  react:
    installed: 19.0.0
    wanted: 19.0.0
  react-native:
    installed: 0.79.1
    wanted: 0.79.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

no stacktrace

MANDATORY Reproducer

https://github.com/roman-zaitsev/rn791-dimensions-api-bug-reproducer

Screenshots and Videos

No response

@natek-dev
Copy link

After testing, this issue was not present on 0.78 and appears only since 0.79.0.

Impacted versions :

  • 0.79.0
  • 0.79.1

Can be reproduced with a basic new project setup.

@devanshsaini11
Copy link
Collaborator

@roman-zaitsev Thanks for sharing this, I was able to reproduce this locally

@devanshsaini11 devanshsaini11 added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 labels Apr 24, 2025
@riteshshukla04
Copy link
Contributor

riteshshukla04 commented Apr 25, 2025

This is fixed on main . So you can expect the fix to be in 0.80.0

@xubmuajkub
Copy link

For a temporary fix, just copy code from this file and replace code in node_modules/react-native/React/CoreModules/RCTDeviceInfo.mm then create a patch npx patch-package react-native and rebuild your project.

To make sure patch-package applies the patch you made, just add it to your script in package.json

{
  "name": "project_name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "postinstall": "npx patch-package",
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: Dimensions Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants