I'll help you create a scalable React Native application structure with authentication flow and drawer navigation. Here's a well-organized system design approach:
Folder Structure
code Copy code
src/
├── components/
│ ├── common/
│ │ ├── Button.js
│ │ ├── Input.js
│ │ └── Header.js
│ └── specific/
│ └── DrawerContent.js
├── screens/
│ ├── auth/
│ │ └── LoginScreen.js
│ ├── home/
│ │ └── HomeScreen.js
│ ├── profile/
│ │ └── ProfileScreen.js
│ └── settings/
│ └── SettingsScreen.js
├── navigation/
│ ├── AuthNavigator.js
│ ├── DrawerNavigator.js
│ └── RootNavigator.js
├── contexts/
│ └── AuthContext.js
├── services/
│ └── authService.js
├── utils/
│ └── constants.js
└── App.js
Implementation
1. App.js
javascript Copy code
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { AuthProvider } from './src/contexts/AuthContext';
import RootNavigator from './src/navigation/RootNavigator';
export default function App() {
return (
<AuthProvider>
<NavigationContainer>
<RootNavigator />
</NavigationContainer>
</AuthProvider>
);
}
2. src/contexts/AuthContext.js
javascript Copy code
import React, { createContext, useState, useContext } from 'react';
const AuthContext = createContext({});
export const useAuth = () => useContext(AuthContext);
export const AuthProvider = ({ children }) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [user, setUser] = useState(null);
const login = async (email, password) => {
// Simulate API call
return new Promise((resolve) => {
setTimeout(() => {
setUser({ email, name: 'John Doe' });
setIsAuthenticated(true);
resolve({ success: true });
}, 1000);
});
};
const logout = () => {
setUser(null);
setIsAuthenticated(false);
};
return (
<AuthContext.Provider value={{ isAuthenticated, user, login, logout }}>
{children}
</AuthContext.Provider>
);
};
3. src/navigation/RootNavigator.js
javascript Copy code
import React from 'react';
import { useAuth } from '../contexts/AuthContext';
import AuthNavigator from './AuthNavigator';
import DrawerNavigator from './DrawerNavigator';
export default function RootNavigator() {
const { isAuthenticated } = useAuth();
return isAuthenticated ? <DrawerNavigator /> : <AuthNavigator />;
}
4. src/navigation/AuthNavigator.js
javascript Copy code
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import LoginScreen from '../screens/auth/LoginScreen';
const Stack = createStackNavigator();
export default function AuthNavigator() {
return (
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Login" component={LoginScreen} />
</Stack.Navigator>
);
}
5. src/navigation/DrawerNavigator.js
javascript Copy code
import React from 'react';
import { createDrawerNavigator } from '@react-navigation/drawer';
import HomeScreen from '../screens/home/HomeScreen';
import ProfileScreen from '../screens/profile/ProfileScreen';
import SettingsScreen from '../screens/settings/SettingsScreen';
import DrawerContent from '../components/specific/DrawerContent';
const Drawer = createDrawerNavigator();
export default function DrawerNavigator() {
return (
<Drawer.Navigator
drawerContent={(props) => <DrawerContent {...props} />}
screenOptions={{
drawerActiveTintColor: '#2196F3',
drawerInactiveTintColor: '#333',
}}
>
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Profile" component={ProfileScreen} />
<Drawer.Screen name="Settings" component={SettingsScreen} />
</Drawer.Navigator>
);
}
6. src/screens/auth/LoginScreen.js
javascript Copy code
import React, { useState } from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
TouchableOpacity,
ActivityIndicator,
Alert,
} from 'react-native';
import { useAuth } from '../../contexts/AuthContext';
export default function LoginScreen() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const { login } = useAuth();
const handleLogin = async () => {
if (!email || !password) {
Alert.alert('Error', 'Please fill in all fields');
return;
}
setLoading(true);
try {
await login(email, password);
} catch (error) {
Alert.alert('Error', 'Login failed');
}
setLoading(false);
};
return (
<View style={styles.container}>
<View style={styles.form}>
<Text style={styles.title}>Welcome Back</Text>
<Text style={styles.subtitle}>Sign in to continue</Text>
<TextInput
style={styles.input}
placeholder="Email"
value={email}
onChangeText={setEmail}
keyboardType="email-address"
autoCapitalize="none"
/>
<TextInput
style={styles.input}
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
<TouchableOpacity
style={styles.button}
onPress={handleLogin}
disabled={loading}
>
{loading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.buttonText}>Login</Text>
)}
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
justifyContent: 'center',
padding: 20,
},
form: {
backgroundColor: '#fff',
padding: 30,
borderRadius: 10,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 5,
},
title: {
fontSize: 28,
fontWeight: 'bold',
color: '#333',
marginBottom: 10,
},
subtitle: {
fontSize: 16,
color: '#666',
marginBottom: 30,
},
input: {
borderWidth: 1,
borderColor: '#ddd',
padding: 15,
marginBottom: 20,
borderRadius: 8,
fontSize: 16,
},
button: {
backgroundColor: '#2196F3',
padding: 15,
borderRadius: 8,
alignItems: 'center',
marginTop: 10,
},
buttonText: {
color: '#fff',
fontSize: 18,
fontWeight: '600',
},
});
7. src/screens/home/HomeScreen.js
javascript Copy code
import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import { useAuth } from '../../contexts/AuthContext';
export default function HomeScreen({ navigation }) {
const { user } = useAuth();
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome, {user?.name}!</Text>
<Text style={styles.subtitle}>This is your home screen</Text>
<TouchableOpacity
style={styles.menuButton}
onPress={() => navigation.openDrawer()}
>
<Text style={styles.menuButtonText}>Open Menu</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
},
welcome: {
fontSize: 24,
fontWeight: 'bold',
color: '#333',
marginBottom: 10,
},
subtitle: {
fontSize: 16,
color: '#666',
marginBottom: 30,
},
menuButton: {
backgroundColor: '#2196F3',
paddingHorizontal: 30,
paddingVertical: 12,
borderRadius: 8,
},
menuButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});
8. src/components/specific/DrawerContent.js
javascript Copy code
import React from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
ScrollView,
} from 'react-native';
import { DrawerContentScrollView } from '@react-navigation/drawer';
import { useAuth } from '../../contexts/AuthContext';
export default function DrawerContent(props) {
const { user, logout } = useAuth();
const handleLogout = () => {
logout();
};
return (
<ScrollView style={styles.container}>
<View style={styles.header}>
<Text style={styles.username}>{user?.name}</Text>
<Text style={styles.email}>{user?.email}</Text>
</View>
<DrawerContentScrollView {...props}>
<View style={styles.menuItems}>
<TouchableOpacity
style={styles.menuItem}
onPress={() => props.navigation.navigate('Home')}
>
<Text style={styles.menuItemText}>Home</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.menuItem}
onPress={() => props.navigation.navigate('Profile')}
>
<Text style={styles.menuItemText}>Profile</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.menuItem}
onPress={() => props.navigation.navigate('Settings')}
>
<Text style={styles.menuItemText}>Settings</Text>
</TouchableOpacity>
</View>
</DrawerContentScrollView>
<TouchableOpacity style={styles.logoutButton} onPress={handleLogout}>
<Text style={styles.logoutText}>Logout</Text>
</TouchableOpacity>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
backgroundColor: '#2196F3',
padding: 20,
paddingTop: 50,
},
username: {
color: '#fff',
fontSize: 20,
fontWeight: 'bold',
},
email: {
color: '#fff',
fontSize: 14,
marginTop: 5,
},
menuItems: {
marginTop: 20,
},
menuItem: {
paddingVertical: 15,
paddingHorizontal: 20,
},
menuItemText: {
fontSize: 16,
color: '#333',
},
logoutButton: {
backgroundColor: '#ff4444',
margin: 20,
padding: 15,
borderRadius: 8,
alignItems: 'center',
},
logoutText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});
9. src/screens/profile/ProfileScreen.js
javascript Copy code
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { useAuth } from '../../contexts/AuthContext';
export default function ProfileScreen() {
const { user } = useAuth();
return (
<View style={styles.container}>
<Text style={styles.title}>Profile</Text>
<View style={styles.infoContainer}>
<Text style={styles.label}>Name:</Text>
<Text style={styles.value}>{user?.name}</Text>
</View>
<View style={styles.infoContainer}>
<Text style={styles.label}>Email:</Text>
<Text style={styles.value}>{user?.email}</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#f5f5f5',
},
title: {
fontSize: 28,
fontWeight: 'bold',
color: '#333',
marginBottom: 30,
},
infoContainer: {
backgroundColor: '#fff',
padding: 15,
borderRadius: 8,
marginBottom: 15,
flexDirection: 'row',
justifyContent: 'space-between',
},
label: {
fontSize: 16,
color: '#666',
fontWeight: '600',
},
value: {
fontSize: 16,
color: '#333',
},
});
10. package.json dependencies
json Copy code
{
"dependencies": {
"@react-navigation/native": "^6.x",
"@react-navigation/stack": "^6.x",
"@react-navigation/drawer": "^6.x",
"react-native-screens": "~3.x",
"react-native-safe-area-context": "4.x",
"react-native-gesture-handler": "~2.x",
"react-native-reanimated": "~3.x"
}
}
To Add More Pages
Create a new screen file in the appropriate folder under src/screens/
Import it in DrawerNavigator.js
Add a new Drawer.Screen entry
Add navigation item in DrawerContent.js
Example for adding a new "About" page:
javascript Copy code
// src/screens/about/AboutScreen.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default function AboutScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>About</Text>
<Text style={styles.content}>Your app description here</Text>
</View>
);
}
// Then add to DrawerNavigator.js:
<Drawer.Screen name="About" component={AboutScreen} />
This structure provides excellent scalability, separation of concerns, and easy maintenance for your React Native application.