Responsive dimensions help your app look good on all screen sizes. They let you get the device's width and height to adjust layouts easily.
Responsive dimensions (Dimensions API) in React Native
import { Dimensions } from 'react-native'; const windowWidth = Dimensions.get('window').width; const windowHeight = Dimensions.get('window').height;
Dimensions.get('window') returns the width and height of the app's usable screen area.
You can also use Dimensions.get('screen') to get the entire screen size including status bars.
const width = Dimensions.get('window').width; const height = Dimensions.get('window').height;
const screenWidth = Dimensions.get('screen').width; const screenHeight = Dimensions.get('screen').height;
const halfWidth = Dimensions.get('window').width / 2;
This app shows a blue box sized to 80% of the screen width and 30% of the screen height. The box's size changes if you rotate the device or use a different screen size.
import React from 'react'; import { View, Text, StyleSheet, Dimensions } from 'react-native'; export default function App() { const windowWidth = Dimensions.get('window').width; const windowHeight = Dimensions.get('window').height; return ( <View style={styles.container}> <View style={[styles.box, { width: windowWidth * 0.8, height: windowHeight * 0.3 }]}> <Text style={styles.text}>Responsive Box</Text> <Text style={styles.text}>Width: {Math.round(windowWidth * 0.8)} px</Text> <Text style={styles.text}>Height: {Math.round(windowHeight * 0.3)} px</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0' }, box: { backgroundColor: '#4a90e2', justifyContent: 'center', alignItems: 'center', borderRadius: 10 }, text: { color: 'white', fontSize: 18, marginVertical: 2 } });
Dimensions values do not update automatically on screen rotation. Use the useWindowDimensions hook for auto-updating dimensions.
Use percentages of screen size for flexible layouts instead of fixed pixel values.
Use Dimensions.get('window') to get screen width and height.
Calculate sizes as fractions of screen dimensions for responsive design.
For dynamic updates on rotation, consider useWindowDimensions hook.
Practice
Dimensions.get('window') return in React Native?Solution
Step 1: Understand Dimensions API purpose
The Dimensions API provides device screen information.Step 2: Identify what get('window') returns
It returns an object containing width and height of the window.Final Answer:
An object with screen width and height -> Option AQuick Check:
Dimensions.get('window') = object with width and height [OK]
- Thinking it returns a function
- Confusing it with device type string
- Assuming it returns orientation boolean
Solution
Step 1: Recall React Native import style
React Native exports Dimensions as a named export.Step 2: Choose correct import syntax
Correct syntax is: import { Dimensions } from 'react-native';Final Answer:
import { Dimensions } from 'react-native'; -> Option AQuick Check:
Named import from 'react-native' = import { Dimensions } from 'react-native'; [OK]
- Using default import without braces
- Importing from wrong module name
- Confusing named and default imports
const { width, height } = Dimensions.get('window');
const boxWidth = width * 0.5;
console.log(boxWidth);If the device screen width is 360 pixels, what will be logged?
Solution
Step 1: Extract width from Dimensions
width is 360 pixels as given.Step 2: Calculate boxWidth as half of width
boxWidth = 360 * 0.5 = 180 pixels.Final Answer:
180 -> Option DQuick Check:
360 * 0.5 = 180 [OK]
- Confusing multiplication with addition
- Using height instead of width
- Misreading 0.5 as 5
import { Dimensions } from 'react-native';
const { width, height } = Dimensions.get('screen');
const boxHeight = width * 0.3;
console.log(boxHeight);Solution
Step 1: Understand difference between 'screen' and 'window'
'screen' includes status bar and navigation, 'window' excludes them.Step 2: Identify impact on layout
Using 'screen' may cause layout to not fit visible area properly.Final Answer:
Using 'screen' instead of 'window' may cause unexpected layout issues -> Option BQuick Check:
'screen' vs 'window' affects layout size [OK]
- Thinking 'screen' and 'window' are the same
- Ignoring layout issues from status bar
- Assuming destructuring is invalid
Solution
Step 1: Understand requirement for dynamic size on rotation
Fixed values or one-time get won't update on rotation.Step 2: Choose hook that updates on dimension changes
useWindowDimensions hook updates width and height on rotation.Step 3: Calculate height based on current width
Set height = width * 0.4 to keep square shape responsive.Final Answer:
Use useWindowDimensions hook to get width and set height equal to width * 0.4 -> Option CQuick Check:
Dynamic hook + calculation = responsive square [OK]
- Using fixed sizes that don't update
- Using Dimensions.get only once
- Confusing 'screen' with 'window' for layout
Ready to prove it?
Pass the test and this pattern moves unknown
