Open In App

How to Get Window Width and Height In React Native ?

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we'll explore two different approaches to obtaining the screen width and height in a React Native application. Scree­n dimensions, including width and height, play a vital role in de­signing user interfaces that adapt se­amlessly to different de­vices and orientations. By understanding these dimensions, deve­lopers empower themselves to create responsive layouts and deliver a cohesive user experience across dive­rse devices.

To give you a better idea of what we’re going to create, let’s see a demo.

Demo

Screen_width_and_height


Step-by-Step Implementation

Step 1: Create a React Native Project

Now, create a project with the following command.

npx create-expo-app app-name --template

Note: 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-name

Project Structure:

Step 2: Run  Application

Start the server by using the following command.

npx expo start

Then, the application will display a QR code.

  • For the Android users,
    • For the Android Emulator, press "a" as mentioned in the image below.
    • For the Physical Device, download the "Expo Go" app from the Play Store. Open the app, and you will see a button labeled "Scan QR Code." Click that button and scan the QR code; it will automatically build the Android app on your device.
  • For iOS users, simply scan the QR code using the Camera app.
  • If you're using a web browser, it will provide a local host link that you can use as mentioned in the image below.

Step 3: Start Coding

Approach 1: Using the Dimensions Module

In this approach, the Dime­nsions module is utilized to retrie­ve the scree­n width and height. By invoking Dimensions.get('window'), the­ dimensions are accesse­d and displayed using the Text compone­nt.

Example: In this example, we are using the above-explained approach.

App.js
import {
  View, // Importing the View component for creating container-like elements
  Text, // Importing the Text component for displaying text
  Dimensions, // Importing Dimensions to get the screen width and height
  StyleSheet, // Importing StyleSheet to create styles for the components
} from "react-native";

const App = () => {
  // Destructuring width and height from the Dimensions API
  const { width, height } = Dimensions.get("window");

  return (
    <View style={styles.container}> {/* Main container with styling */}
      <Text style={styles.heading}> {/* Heading text */}
        Geeksforgeeks
      </Text>
      <View style={styles.dimensionContainer}> {/* Container for displaying dimensions */}
        <Text style={styles.dimensionText}> {/* Text to display screen width */}
          Screen Width: {width}
        </Text>
        <Text style={styles.dimensionText}> {/* Text to display screen height */}
          Screen Height: {height}
        </Text>
      </View>
    </View>
  );
};

// Defining styles for the components
const styles = StyleSheet.create({
  container: {
    flex: 1, // Makes the container take up the full screen
    justifyContent: "center", // Centers content vertically
    alignItems: "center", // Centers content horizontally
    backgroundColor: "#f0f0f0", // Light gray background color
  },
  heading: {
    fontSize: 30, // Large font size for the heading
    fontWeight: "bold", // Bold font weight
    marginBottom: 30, // Space below the heading
    color: "green", // Green text color
  },
  dimensionContainer: {
    backgroundColor: "white", // White background for the dimensions container
    borderRadius: 10, // Rounded corners
    padding: 20, // Padding inside the container
    shadowColor: "#000", // Shadow color
    shadowOffset: { // Shadow offset for iOS
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25, // Shadow opacity for iOS
    shadowRadius: 3.84, // Shadow radius for iOS
    elevation: 5, // Shadow elevation for Android
  },
  dimensionText: {
    fontSize: 18, // Font size for dimension text
    marginBottom: 10, // Space below each dimension text
    color: "#666", // Gray text color
  },
});

export default App; // Exporting the App component as the default export


Output:

Using_the_Dimensions_Module


Approach 2: Using useWindowDimensions

In this approach, we'll use the useWindow­Dimensions hook to effort­lessly access the width and height of the screen.

Example: The code defines a React Native component named 'App' that showcases the dimen­sions of the device screen. It imports necessary compo­nents from 'react-native' while utilizing the useWindow­Dimensions hook to obtain both the width and height of the scree­n.

App.js
import {
  View, // Importing the View component for creating container elements
  Text, // Importing the Text component for displaying text
  useWindowDimensions, // Importing the hook to get screen dimensions
  StyleSheet, // Importing StyleSheet for creating styles
} from "react-native"; // Importing from the react-native library

// Main functional component of the app
const App = () => {
  // Destructuring width and height from useWindowDimensions hook
  const { width, height } = useWindowDimensions();

  return (
    // Main container view
    <View style={styles.container}>
      {/* Displaying the title text */}
      <Text style={styles.text}>Geeksforgeeks</Text>
      {/* Container for displaying screen dimensions */}
      <View style={styles.dimensionContainer}>
        {/* Displaying the screen width */}
        <Text style={styles.dimensionText}>
          Screen Width: {width}
        </Text>
        {/* Displaying the screen height */}
        <Text style={styles.dimensionText}>
          Screen Height: {height}
        </Text>
      </View>
    </View>
  );
};

// Defining styles for the components
const styles = StyleSheet.create({
  container: {
    flex: 1, // Makes the container take the full screen
    justifyContent: "center", // Centers content vertically
    alignItems: "center", // Centers content horizontally
    backgroundColor: "#f0f0f0", // Light gray background color
  },
  text: {
    fontSize: 30, // Large font size for the title
    fontWeight: "bold", // Bold font weight
    marginBottom: 20, // Space below the title
    color: "green", // Green text color
  },
  dimensionContainer: {
    backgroundColor: "white", // White background for the dimensions container
    borderRadius: 10, // Rounded corners
    padding: 20, // Padding inside the container
    shadowColor: "#000", // Shadow color
    shadowOffset: {
      width: 0, // Horizontal shadow offset
      height: 2, // Vertical shadow offset
    },
    shadowOpacity: 0.25, // Shadow transparency
    shadowRadius: 3.84, // Shadow blur radius
    elevation: 5, // Elevation for Android shadow
  },
  dimensionText: {
    fontSize: 18, // Font size for dimension text
    marginBottom: 10, // Space below each dimension text
    color: "#666", // Gray text color
  },
});

// Exporting the App component as the default export
export default App;


Output:

Using_useWindowDimensions



Next Article

Similar Reads