Master React Native core components and styling techniques for beautiful mobile interfaces.
Master React Native core components and styling techniques for beautiful mobile interfaces.
Deep dive into the core React Native components: View, Text, and Image, understanding their properties and use cases
Content by: Ronak Macwan
React Native Developer
The View component is the most fundamental building block in React Native. It's a container that supports layout with flexbox, styling, touch handling, and accessibility controls. It maps to UIView on iOS and View on Android.
import React from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
const CardComponent = () => {
return (
<View style={styles.card}>
<Image
source={{ uri: 'https://example.com/avatar.jpg' }}
style={styles.avatar}
resizeMode="cover"
/>
<View style={styles.content}>
<Text style={styles.title}>John Doe</Text>
<Text style={styles.subtitle} numberOfLines={2}>
Software Developer with 5 years of experience
</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
card: {
flexDirection: 'row',
padding: 16,
backgroundColor: '#fff',
borderRadius: 8,
margin: 8,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
avatar: {
width: 60,
height: 60,
borderRadius: 30,
},
content: {
flex: 1,
marginLeft: 12,
},
title: {
fontSize: 18,
fontWeight: 'bold',
color: '#333',
},
subtitle: {
fontSize: 14,
color: '#666',
marginTop: 4,
},
});
export default CardComponent;Test your understanding of this topic:
Master React Native styling with StyleSheet and Flexbox layout system for responsive and beautiful mobile interfaces
Content by: Ronak Macwan
React Native Developer
StyleSheet provides an abstraction layer similar to CSS stylesheets. It creates style objects with an ID that can be referenced instead of creating new style objects each time. This improves performance and enables better debugging.
import React from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
const { width } = Dimensions.get('window');
const ResponsiveLayout = () => {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.title}>Responsive Layout</Text>
</View>
<View style={styles.content}>
<View style={styles.sidebar}>
<Text>Sidebar</Text>
</View>
<View style={styles.main}>
<Text>Main Content</Text>
</View>
</View>
<View style={styles.footer}>
<Text>Footer</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
},
header: {
height: 60,
backgroundColor: '#2196F3',
justifyContent: 'center',
alignItems: 'center',
},
title: {
color: 'white',
fontSize: 18,
fontWeight: 'bold',
},
content: {
flex: 1,
flexDirection: width > 768 ? 'row' : 'column',
},
sidebar: {
width: width > 768 ? 200 : '100%',
height: width > 768 ? '100%' : 100,
backgroundColor: '#E3F2FD',
justifyContent: 'center',
alignItems: 'center',
},
main: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
},
footer: {
height: 50,
backgroundColor: '#1976D2',
justifyContent: 'center',
alignItems: 'center',
},
});
export default ResponsiveLayout;Test your understanding of this topic:
Understanding device dimensions, screen sizes, and how to create layouts that work across different devices and orientations
Content by: Ronak Macwan
React Native Developer
Use Dimensions API to get the screen dimensions and create responsive layouts. Dimensions.get('window') gives you the window dimensions, while Dimensions.get('screen') gives you the screen dimensions.
Listen to orientation changes using Dimensions.addEventListener to update your layout when the device rotates between portrait and landscape modes.
Use SafeAreaView or react-native-safe-area-context to handle safe areas on devices with notches, status bars, and home indicators. This ensures your content is not hidden behind system UI elements.
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
const ResponsiveDimensions = () => {
const [dimensions, setDimensions] = useState(Dimensions.get('window'));
useEffect(() => {
const subscription = Dimensions.addEventListener('change', ({ window }) => {
setDimensions(window);
});
return () => subscription?.remove();
}, []);
const isTablet = dimensions.width >= 768;
const isLandscape = dimensions.width > dimensions.height;
return (
<View style={styles.container}>
<Text style={styles.title}>
Device: {isTablet ? 'Tablet' : 'Phone'}
</Text>
<Text style={styles.subtitle}>
Orientation: {isLandscape ? 'Landscape' : 'Portrait'}
</Text>
<Text style={styles.info}>
Width: {Math.round(dimensions.width)}px
</Text>
<Text style={styles.info}>
Height: {Math.round(dimensions.height)}px
</Text>
<View style={[
styles.content,
isTablet && styles.tabletContent,
isLandscape && styles.landscapeContent
]}>
<Text>Responsive Content</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 10,
},
subtitle: {
fontSize: 18,
marginBottom: 20,
},
info: {
fontSize: 16,
marginBottom: 5,
},
content: {
width: '100%',
height: 200,
backgroundColor: '#E3F2FD',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 8,
marginTop: 20,
},
tabletContent: {
height: 300,
},
landscapeContent: {
flexDirection: 'row',
},
});
export default ResponsiveDimensions;Test your understanding of this topic:
Learn how to apply platform-specific styles and components to create native-feeling interfaces on both iOS and Android
Content by: Ronak Macwan
React Native Developer
Use Platform.OS to detect the current platform and apply different styles or components based on whether the app is running on iOS or Android.
Create platform-specific styles using Platform.select() to provide different style objects for iOS and Android, ensuring your app feels native on each platform.
Use Platform.select() to render different components for iOS and Android, such as different navigation patterns, button styles, or input components.
import React from 'react';
import { View, Text, StyleSheet, Platform } from 'react-native';
const PlatformSpecificExample = () => {
return (
<View style={styles.container}>
<Text style={styles.title}>Platform-Specific Styling</Text>
<View style={styles.buttonContainer}>
<Text style={styles.buttonText}>
{Platform.OS === 'ios' ? 'iOS Button' : 'Android Button'}
</Text>
</View>
<Text style={styles.description}>
This text has different styling on iOS and Android
</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: Platform.OS === 'ios' ? '#F2F2F7' : '#FAFAFA',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 30,
color: Platform.OS === 'ios' ? '#000' : '#333',
},
buttonContainer: {
backgroundColor: Platform.select({
ios: '#007AFF',
android: '#2196F3',
}),
paddingHorizontal: 20,
paddingVertical: 12,
borderRadius: Platform.OS === 'ios' ? 8 : 4,
marginBottom: 20,
},
buttonText: {
color: 'white',
fontSize: 16,
fontWeight: Platform.OS === 'ios' ? '600' : '500',
},
description: {
fontSize: 16,
textAlign: 'center',
color: Platform.select({
ios: '#8E8E93',
android: '#757575',
}),
fontFamily: Platform.select({
ios: 'System',
android: 'Roboto',
}),
},
});
export default PlatformSpecificExample;Test your understanding of this topic:
Continue your learning journey and master the next set of concepts.
Continue to Module 3