To add a settings button at the top right of your ProfileScreen.js, similar to TikTok's three horizontal lines (commonly known as a hamburger menu), you'll need to integrate a header button into your screen's navigation header. When this button is pressed, it will navigate the user to the SettingsScreen.js.
Assuming you're using React Navigation (which is the most common navigation library for React Native), here's a step-by-step guide to implementing this feature:
1. Ensure SettingsScreen.js Is Registered in Your Navigator
First, make sure that SettingsScreen.js is properly registered in your navigation stack. This ensures that you can navigate to it from the ProfileScreen.
// navigation/AppNavigator.js or wherever your navigator is defined
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import ProfileScreen from '../screens/ProfileScreen';
import SettingsScreen from '../screens/SettingsScreen';
// Import other screens as needed
const Stack = createStackNavigator();
const AppNavigator = () => {
return (
<Stack.Navigator>
{/* Other screens */}
<Stack.Screen name="Profile" component={ProfileScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
{/* Other screens */}
</Stack.Navigator>
);
};
export default AppNavigator;
2. Install Necessary Icon Libraries
Since you want to use an icon with three horizontal lines, you can use react-native-vector-icons. If you haven't installed it yet, follow these steps:
# Using npm
npm install react-native-vector-icons
# Using yarn
yarn add react-native-vector-icons
Linking Icons (If Necessary):
For React Native versions below 0.60, you'll need to link the library:
react-native link react-native-vector-icons
For React Native 0.60 and above, linking is handled automatically.
3. Modify ProfileScreen.js to Include the Settings Button
We'll use the useLayoutEffect hook to set the header options dynamically, adding a button to the header's right side.
Here's the updated ProfileScreen.js:
// screens/ProfileScreen.js
import React, { useContext, useLayoutEffect } from 'react';
import { View, StyleSheet, ScrollView, Alert, ActivityIndicator, TouchableOpacity } from 'react-native';
import { Title, Caption, Text, Button } from 'react-native-paper';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // Ensure Icon is imported
import { AuthContext } from '../context/AuthContext';
import { useTheme } from 'react-native-paper';
/**
* ProfileScreen Component
* Displays the authenticated user's profile information.
*/
const ProfileScreen = ({ navigation }) => {
const { user, signOut, loading } = useContext(AuthContext);
const theme = useTheme(); // Access the current theme
/**
* 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 }
);
};
/**
* Set up the header with the settings button
*/
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => navigation.navigate('Settings')}
style={styles.headerButton}
accessibilityLabel="Settings"
accessibilityHint="Navigates to the Settings screen"
>
<Icon name="menu" size={28} color={theme.colors.text} />
</TouchableOpacity>
),
headerTitle: 'Profile', // Optional: Customize the header title
});
}, [navigation, theme.colors.text]);
/**
* If the app is still loading user data, show a loading indicator
*/
if (loading) {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color={theme.colors.primary} />
</View>
);
}
/**
* If no user is logged in, prompt the user to log in
*/
if (!user) {
return (
<View style={styles.container}>
<Text>No user data available. Please log in.</Text>
<Button
mode="contained"
onPress={() => navigation.navigate('Login')}
style={styles.loginButton}
accessibilityLabel="Navigate to Login"
>
Go to Login
</Button>
</View>
);
}
/**
* Render the user's profile information
*/
return (
<ScrollView
contentContainerStyle={[styles.container, { backgroundColor: theme.colors.background }]}
>
{/* User Information Section */}
<View style={styles.userInfoSection}>
{user.name ? (
<Title style={[styles.title, { color: theme.colors.text }]}>{user.name}</Title>
) : (
<Title style={[styles.title, { color: theme.colors.text }]}>User ID: {user.id}</Title>
)}
{user.email && (
<Caption style={[styles.caption, { color: theme.colors.text }]}>{user.email}</Caption>
)}
</View>
{/* Logout Button */}
<View style={styles.logoutSection}>
<Button
mode="contained"
icon="logout"
onPress={handleLogout}
style={styles.logoutButton}
contentStyle={styles.buttonContent}
accessibilityLabel="Logout"
accessibilityHint="Logs you out of the application"
>
Logout
</Button>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
// Removed alignItems and justifyContent from container
container: {
flexGrow: 1, // Use flexGrow to allow content to scroll if necessary
padding: 20,
// justifyContent: 'center', // Removed: should not be applied here
// alignItems: 'center', // Removed: should not be applied here
},
userInfoSection: {
paddingHorizontal: 20,
paddingVertical: 40,
alignItems: 'center', // Center the user info within this View
backgroundColor: 'transparent',
},
title: {
fontSize: 26,
fontWeight: 'bold',
},
caption: {
fontSize: 16,
lineHeight: 16,
marginTop: 5,
},
logoutSection: {
paddingHorizontal: 20,
marginTop: 30,
alignItems: 'center', // Center the logout button within this View
},
logoutButton: {
width: '100%', // Make the button stretch across the container
borderRadius: 5,
// backgroundColor: undefined, // Not needed: using theme colors
},
buttonContent: {
height: 50, // Ensures consistent button height
justifyContent: 'center',
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
loginButton: {
marginTop: 20,
borderRadius: 25,
paddingHorizontal: 20,
paddingVertical: 10,
},
headerButton: {
marginRight: 15, // Adjust spacing as needed
},
});
export default ProfileScreen;
4. Explanation of the Changes
-
Importing useLayoutEffect and Icon:
useLayoutEffect is used to set the navigation options before the component renders.
Icon from react-native-vector-icons/MaterialCommunityIcons provides the hamburger menu icon.
-
Setting the Header Right Button:
- Inside the
useLayoutEffect, we use navigation.setOptions to define the header's right component.
- A
TouchableOpacity wraps the Icon to make it clickable. When pressed, it navigates to the Settings screen.
- The
headerTitle is optionally set to 'Profile', but you can customize it or remove it if it's already set elsewhere.
-
Styling the Header Button:
- Added a
headerButton style to provide spacing (marginRight) so that the icon isn't flush against the screen's edge.
-
Accessibility:
- Included
accessibilityLabel and accessibilityHint to improve accessibility, making it clear to users (including those using screen readers) what the button does.
5. Ensure SettingsScreen.js Is Properly Implemented
Make sure that your SettingsScreen.js is implemented and exported correctly. Here's a simple example:
// screens/SettingsScreen.js
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { Text, Button, useTheme } from 'react-native-paper';
/**
* SettingsScreen Component
* Displays the settings options for the user.
*/
const SettingsScreen = ({ navigation }) => {
const theme = useTheme();
return (
<View style={[styles.container, { backgroundColor: theme.colors.background }]}>
<Text style={styles.text}>Settings Screen</Text>
{/* Add your settings options here */}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 18,
},
});
export default SettingsScreen;
6. Additional Considerations
-
Icon Size and Color: Adjust the size and color props of the Icon component to match your app's design. In the example, the icon size is set to 28, and the color is derived from the current theme's text color.
-
Header Styling: If you want to customize the header's appearance (e.g., background color, title style), you can do so within the navigation.setOptions method.
-
Using React Native Paper's IconButton: If you prefer using React Native Paper's IconButton instead of TouchableOpacity with Icon, you can replace the TouchableOpacity with IconButton like so:
import { IconButton } from 'react-native-paper';
// Inside useLayoutEffect
headerRight: () => (
<IconButton
icon="menu"
size={28}
onPress={() => navigation.navigate('Settings')}
accessibilityLabel="Settings"
accessibilityHint="Navigates to the Settings screen"
/>
),
This approach simplifies the code by using a component that's already optimized for icons and touch interactions.
-
Navigation Stack Configuration: Ensure that your navigation stack allows for SettingsScreen to be navigated to from ProfileScreen. If you're using nested navigators (e.g., a tab navigator inside a stack navigator), adjust the navigation accordingly.
-
Testing on Different Devices: Verify how the header and the settings button appear on different device sizes and orientations to ensure a consistent user experience.
By following these steps, you should have a settings button integrated into your ProfileScreen's header, enhancing the navigation and user experience of your application.