![]() |
VOOZH | about |
In React Native, obtaining the screen width and height is essential for creating responsive and adaptive user interfaces. It helps developers design layouts that adjust properly across different devices and screen orientations.
Now, create a project with the following command.
npx create-expo-app app-name --templateNote: Replace the app-name with your app name for example : react-native-demo-app
Next, you might be asked to choose a template. Select one based on your preference as shown in the image below. I am selecting the blank template because it will generate a minimal app, as clean as an empty canvas in JavaScript.
It completes the project creation and displays a message: "Your Project is ready!" as shown in the image below.
Now go into your project folder, i.e., react-native-demo
cd app-nameStart the server by using the following command.
npx expo startThen, the application will display a QR code.
1. For the Android users,
2. For iOS users, simply scan the QR code using the Camera app.
3. If you're using a web browser, it will provide a local host link that you can use as mentioned in the image below.
Approach 1: Using the Dimensions Module
In this approach, the Dimensions module is utilized to retrieve the screen width and height. By invoking Dimensions.get('window'), the dimensions are accessed and displayed using the Text component.
Output:
In this approach, we'll use the useWindowDimensions hook to effortlessly access the width and height of the screen.
Example: The code defines a React Native component named 'App' that showcases the dimensions of the device screen. It imports necessary components from 'react-native' while utilizing the useWindowDimensions hook to obtain both the width and height of the screen.
Output: