Separating your component logic from styles is a great practice for maintaining cleaner and more manageable code. You can achieve this by moving the StyleSheet definitions to a separate file and importing them into your component. Below are the steps to refactor your SettingsScreen.js by separating the styles.
Step 1: Create a Separate Styles File
Create a new file named SettingsScreen.styles.js in the same directory as your SettingsScreen.js, or in a dedicated styles folder within your screens directory for better organization.
Path Example:
frontend/
└── screens/
├── SettingsScreen.js
└── styles/
└── SettingsScreen.styles.js
Step 2: Move the Styles to the New File
Copy the StyleSheet.create object from SettingsScreen.js to SettingsScreen.styles.js. Make sure to export the styles so they can be imported into your component.
frontend/screens/styles/SettingsScreen.styles.js
// frontend/screens/styles/SettingsScreen.styles.js
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
header: {
fontSize: 24,
marginBottom: 16,
},
themeOption: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 12,
paddingHorizontal: 16,
borderWidth: 1,
borderRadius: 8,
marginVertical: 6,
},
themeText: {
fontSize: 18,
},
selectedText: {
fontSize: 16,
},
logoutContainer: {
marginTop: 20,
alignItems: 'center',
},
logoutButton: {
width: '100%',
paddingVertical: 10,
borderRadius: 8,
},
buttonContent: {
height: 50,
justifyContent: 'center',
},
});
export default styles;
Step 3: Import the Styles into Your Component
Now, update your SettingsScreen.js to import the styles from the new file.
frontend/screens/SettingsScreen.js
import React, { useContext } from 'react';
import { View, StyleSheet, Alert, ScrollView, TouchableOpacity } from 'react-native';
import { Text, Button, Divider } from 'react-native-paper';
import { ThemeContext } from '../context/ThemeContext';
import { AuthContext } from '../context/AuthContext'; // Import AuthContext
import styles from './styles/SettingsScreen.styles'; // Import the separated styles
const SettingsScreen = () => {
const { theme, themeName, setThemeByName } = useContext(ThemeContext);
const { signOut } = useContext(AuthContext);
// Handler for the logout button
const handleLogout = () => {
Alert.alert(
'Logout',
'Are you sure you want to logout?',
[
{ text: 'Cancel', style: 'cancel' },
{
text: 'Logout',
style: 'destructive',
onPress: () => signOut(),
},
],
{ cancelable: true }
);
};
// List of themes available
const themesList = [
{ name: 'Light', key: 'light' },
{ name: 'Dark', key: 'dark' },
{ name: 'Deep Navy & Gold', key: 'navyGold' },
{ name: 'Black & Platinum', key: 'blackPlatinum' },
{ name: 'Emerald Green & Gold', key: 'emeraldGold' },
{ name: 'Charcoal & Rose Gold', key: 'charcoalRoseGold' },
];
return (
<ScrollView style={[styles.container, { backgroundColor: theme.colors.background }]}>
<Text style={[styles.header, { color: theme.colors.text }]}>Select Theme</Text>
{themesList.map((item) => (
<TouchableOpacity
key={item.key}
style={[
styles.themeOption,
{ borderColor: theme.colors.border },
themeName === item.key && { borderColor: theme.colors.accent },
]}
onPress={() => setThemeByName(item.key)}
>
<Text style={[styles.themeText, { color: theme.colors.text }]}>
{item.name}
</Text>
{themeName === item.key && (
<Text style={[styles.selectedText, { color: theme.colors.accent }]}>
(Selected)
</Text>
)}
</TouchableOpacity>
))}
<Divider style={{ backgroundColor: theme.colors.border, marginVertical: 20 }} />
{/* Logout Button */}
<View style={styles.logoutContainer}>
<Button
mode="contained"
icon="logout"
onPress={handleLogout}
style={styles.logoutButton}
contentStyle={styles.buttonContent}
labelStyle={{ color: theme.colors.onSurface }}
accessibilityLabel="Logout"
accessibilityHint="Logs you out of the application"
color={theme.colors.primary}
>
Logout
</Button>
</View>
</ScrollView>
);
};
export default SettingsScreen;
Step 4: Verify the Refactoring
Ensure that your project runs correctly after refactoring. If you encounter any issues, double-check the following:
- Import Paths: Ensure that the path to
SettingsScreen.styles.js is correct in your import statement.
- Export & Import Syntax: Make sure you're using the correct syntax for exporting and importing the styles.
- Style References: Verify that all style references in
SettingsScreen.js correspond to the definitions in SettingsScreen.styles.js.
Benefits of Separating Styles
- Improved Readability: Keeping styles separate from logic makes your component files cleaner and easier to read.
- Reusability: If multiple components share the same styles, separating them allows for easier reuse.
- Maintainability: Managing and updating styles becomes simpler when they are isolated from component logic.
- Collaboration: Teams can work on styles and functionality independently without causing merge conflicts.
Optional: Using TypeScript for Enhanced Type Safety
If your project uses TypeScript, you can add type definitions to your styles for better type safety. Here's how you can modify the styles file:
frontend/screens/styles/SettingsScreen.styles.ts
import { StyleSheet, ViewStyle, TextStyle, StyleProp } from 'react-native';
interface Styles {
container: StyleProp<ViewStyle>;
header: StyleProp<TextStyle>;
themeOption: StyleProp<ViewStyle>;
themeText: StyleProp<TextStyle>;
selectedText: StyleProp<TextStyle>;
logoutContainer: StyleProp<ViewStyle>;
logoutButton: StyleProp<ViewStyle>;
buttonContent: StyleProp<ViewStyle>;
}
const styles: Styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
header: {
fontSize: 24,
marginBottom: 16,
},
themeOption: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 12,
paddingHorizontal: 16,
borderWidth: 1,
borderRadius: 8,
marginVertical: 6,
},
themeText: {
fontSize: 18,
},
selectedText: {
fontSize: 16,
},
logoutContainer: {
marginTop: 20,
alignItems: 'center',
},
logoutButton: {
width: '100%',
paddingVertical: 10,
borderRadius: 8,
},
buttonContent: {
height: 50,
justifyContent: 'center',
},
});
export default styles;
This addition ensures that any discrepancies in style usage are caught during development, enhancing the robustness of your codebase.
Conclusion
By separating your styles from your component logic, you enhance the maintainability and readability of your code. This practice is especially beneficial as your project scales. Following the steps above will help you achieve a cleaner and more organized code structure in your Expo project.
Feel free to reach out if you have any further questions or need additional assistance!