Build: Responsive Box Screen
This screen shows a colored box that adjusts its size based on the device screen width using the Dimensions API.
Target UI
------------------------- | | | | | [ BOX ] | | | | | -------------------------
------------------------- | | | | | [ BOX ] | | | | | -------------------------
import React from 'react'; import { View, Text, StyleSheet, Dimensions } from 'react-native'; export default function ResponsiveBoxScreen() { // TODO: Get screen width using Dimensions API // TODO: Calculate box size as 50% of screen width return ( <View style={styles.container}> {/* TODO: Add a blue box with responsive size and centered text */} </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', }, });
import React from 'react'; import { View, Text, StyleSheet, Dimensions } from 'react-native'; export default function ResponsiveBoxScreen() { const screenWidth = Dimensions.get('window').width; const boxSize = screenWidth * 0.5; return ( <View style={styles.container}> <View style={[styles.box, { width: boxSize, height: boxSize }]}> <Text style={styles.boxText}>Responsive Box</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', }, box: { backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', borderRadius: 8 }, boxText: { color: 'white', fontSize: 16, fontWeight: 'bold' } });
We use Dimensions.get('window').width to get the current screen width. Then we calculate boxSize as half of that width to keep the box square and responsive.
The box is styled with a blue background and centered text. We use flexbox properties justifyContent and alignItems to center the box and the text inside it.
This approach ensures the box size changes automatically on different screen sizes, making the UI responsive.
------------------------- | | | | | +-------------+ | | | Responsive | | | | Box | | | +-------------+ | | | -------------------------
Dimensions.get('window') return in React Native?const { width, height } = Dimensions.get('window');
const boxWidth = width * 0.5;
console.log(boxWidth);import { Dimensions } from 'react-native';
const { width, height } = Dimensions.get('screen');
const boxHeight = width * 0.3;
console.log(boxHeight);Pass the test and this pattern moves unknown