Exercise 1:
Here's an example of how you can achieve the desired layout using React Native and Expo:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const Exercise1 = () => {
return (
<View style={styles.container}>
<Text style={styles.header}>Here are my boxes</Text>
<View style={styles.boxContainer}>
<Rectangle color="#2aa198" name="Cyan: #2aa198" />
<Rectangle color="#268bd2" name="Blue: #268bd2" />
<Rectangle color="#d33682" name="Magenta: #d33682" />
<Rectangle color="#cb4b16" name="Orange: #cb4b16" />
</View>
</View>
);
};
const Rectangle = ({ color, name }) => {
return (
<View style={[styles.rectangle, { backgroundColor: color }]}>
<Text style={styles.rectangleText}>{name}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
alignItems: 'center',
},
header: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 16,
},
boxContainer: {
justifyContent: 'center',
},
rectangle: {
width: 200,
height: 50,
marginBottom: 8,
justifyContent: 'center',
alignItems: 'center',
},
rectangleText: {
color: 'white',
fontWeight: 'bold',
},
});
export default Exercise1;
You can use the Exercise1 component in your main app file to display the header and rectangles with their respective colors and names.
Exercise 2:
Here's an example of how you can create three centered squares with unique background colors using shared styles:
import React from 'react';
import { View, StyleSheet } from 'react-native';
const Exercise2 = () => {
return (
<View style={styles.container}>
<View style={[styles.square, styles.square1]} />
<View style={[styles.square, styles.square2]} />
<View style={[styles.square, styles.square3]} />
</View>
);
};
const sharedStyles = StyleSheet.create({
square: {
width: 100,
height: 100,
margin: 8,
},
});
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
square1: {
backgroundColor: 'red',
},
square2: {
backgroundColor: 'green',
},
square3: {
backgroundColor: 'blue',
},
});
export default Exercise2;
You can use the Exercise2 component in your main app file to display the three centered squares with their respective colors.
Exercise 3:
Here's an example of how you can create a profile screen layout using the flex property:
import React from 'react';
import { View, Image, Text, StyleSheet } from 'react-native';
const Exercise3 = () => {
return (
<View style={styles.container}>
<View style={styles.profilePictureContainer}>
<Image
source={require('./profile-picture.jpg')}
style={styles.profilePicture}
/>
</View>
<View style={styles.userInfoContainer}>
<Text style={styles.name}>John Doe</Text>
<Text style={styles.bio}>Software Developer</Text>
</View>
<View style={
styles.postsContainer}>
<Text style={styles.postsTitle}>Posts</Text>
<Text style={styles.post}>Post 1</Text>
<Text style={styles.post}>Post 2</Text>
<Text style={styles.post}>Post 3</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
profilePictureContainer: {
alignItems: 'center',
marginTop: 16,
},
profilePicture: {
width: 150,
height: 150,
borderRadius: 75,
},
userInfoContainer: {
alignItems: 'center',
marginTop: 16,
},
name: {
fontSize: 24,
fontWeight: 'bold',
},
bio: {
fontSize: 16,
marginTop: 8,
},
postsContainer: {
flex: 1,
marginTop: 16,
},
postsTitle: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 8,
},
post: {
fontSize: 16,
marginBottom: 4,
},
});
export default Exercise3;
You can use the Exercise3 component in your main app file to display the profile screen layout.
Exercise 4:
Here's an example of how you can create a contact list using the FlatList component:
import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
const ContactScreen = () => {
const contacts = [
{ id: '1', name: 'John Doe', phoneNumber: '123-456-7890' },
{ id: '2', name: 'Jane Smith', phoneNumber: '987-654-3210' },
{ id: '3', name: 'Alice Johnson', phoneNumber: '555-123-4567' },
];
const renderContactItem = ({ item }) => (
<View style={styles.item}>
<Text style={styles.name}>{item.name}</Text>
<Text style={styles.phoneNumber}>{item.phoneNumber}</Text>
</View>
);
return (
<View style={styles.container}>
<FlatList
data={contacts}
renderItem={renderContactItem}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={() => <View style={styles.separator} />}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
item: {
paddingVertical: 8,
},
name: {
fontSize: 16,
fontWeight: 'bold',
},
phoneNumber: {
fontSize: 14,
color: '#666',
},
separator: {
height: 1,
backgroundColor: '#ccc',
},
});
export default ContactScreen;
You can use the ContactScreen component in your main app file to display the contact list using the FlatList component.
Exercise 5:
Here's an example of how you can group contacts by the first letter of their names using the SectionList component:
import React from 'react';
import { View, Text, SectionList, StyleSheet } from 'react-native';
const SectionContactScreen = () => {
const contacts = [
{ id: '1', name: 'John Doe', phoneNumber: '123-456-7890' },
{ id: '2', name: 'Jane Smith', phoneNumber: '987-654-3210' },
{ id: '3', name: 'Alice Johnson', phoneNumber: '555-123-4567' },
// ...more contacts
];
const contactsBySection = contacts.reduce((sections, contact) => {
const firstLetter = contact.name.charAt(0).toUpperCase();
if (!sections[firstLetter]) {
sections[firstLetter] = { title: firstLetter, data: [] };
}
sections[firstLetter].data.push(contact);
return sections;
}, {});
const sections = Object.values(contactsBySection);
const renderContactItem = ({ item }) => (
<View style={styles.item}>
<Text style={styles.name}>{item.name}</Text>
<Text style={styles.phoneNumber}>{item.phoneNumber}</Text>
</View>
);
const renderSectionHeader = ({ section }) => (
<Text style={styles.sectionHeader}>{section.title}</Text>
);
return (
<View style={styles.container}>
<SectionList
sections={sections}
renderItem={renderContactItem}
renderSectionHeader={renderSectionHeader}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={() => <View style={styles.separator} />}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
item: {
paddingVertical: 8,
},
name: {
fontSize: 16,
fontWeight: 'bold',
},
phoneNumber: {
fontSize: 14,
color: '#666',
},
sectionHeader: {
fontSize: 18,
fontWeight: 'bold',
backgroundColor: '#f5f5f5',
paddingVertical: 8,
paddingHorizontal: 16,
},
separator: {
height: 1,
backgroundColor: '#ccc',
},
});
export default SectionContactScreen;
You can use the SectionContactScreen component in your main app file to display the contact list grouped by the first letter of their names using the SectionList component.