To render the MainNavigator in a React Native Realm Login implementation, you’ll need to set up your application to handle user authentication and navigation. Here’s a step-by-step guide:
Step 1: Install Dependencies
First, ensure you have the necessary dependencies installed:
bash
Copy code
npm install @react-navigation/native @react-navigation/stack react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-navigation/native-stack realm
Step 2: Set Up Navigation
Create a navigation setup using React Navigation.
AppNavigator.js
Create a file named AppNavigator.js to define your navigation structure.
javascript
Copy code
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import LoginScreen from './screens/LoginScreen';
import HomeScreen from './screens/HomeScreen';
const Stack = createStackNavigator();
const AppNavigator = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Login" component={LoginScreen} />
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default AppNavigator;
Step 3: Create Login and Home Screens
Create LoginScreen.js and HomeScreen.js in a screens directory.
LoginScreen.js
Handle user login and authentication with Realm.
javascript
Copy code
import React, { useState } from 'react';
import { View, TextInput, Button, StyleSheet } from 'react-native';
import Realm from 'realm';
const app = new Realm.App({ id: 'your-realm-app-id' }); // Replace with your Realm app ID
const LoginScreen = ({ navigation }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleLogin = async () => {
try {
const credentials = Realm.Credentials.emailPassword(email, password);
const user = await app.logIn(credentials);
if (user) {
navigation.navigate('Home');
}
} catch (error) {
console.error('Failed to log in', error);
}
};
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder="Email"
value={email}
onChangeText={setEmail}
keyboardType="email-address"
/>
<TextInput
style={styles.input}
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
<Button title="Log In" onPress={handleLogin} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 20,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 12,
paddingHorizontal: 8,
},
});
export default LoginScreen;
HomeScreen.js
Create a simple home screen that the user sees after logging in.
javascript
Copy code
import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import Realm from 'realm';
const HomeScreen = ({ navigation }) => {
const handleLogout = async () => {
const user = Realm.App.currentUser;
if (user) {
await user.logOut();
navigation.navigate('Login');
}
};
return (
<View style={styles.container}>
<Text>Welcome to the Home Screen!</Text>
<Button title="Log Out" onPress={handleLogout} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default HomeScreen;
Step 4: Render the Navigator in App.js
Finally, render the AppNavigator in your App.js.
App.js
javascript
Copy code
import React from 'react';
import AppNavigator from './AppNavigator';
import { LogBox } from 'react-native';
// Ignore all log notifications
LogBox.ignoreAllLogs();
const App = () => {
return <AppNavigator />;
};
export default App;
Step 5: Configure the Realm App ID
Ensure you have the correct Realm App ID in your LoginScreen.js file:
javascript
Copy code
const app = new Realm.App({ id: 'your-realm-app-id' });
Replace 'your-realm-app-id' with the actual App ID from your Realm application.
Step 6: Run Your App
Run your React Native application:
bash
Copy code
npx react-native run-android
# or
npx react-native run-ios
This setup creates a basic login flow using Realm for authentication and React Navigation for managing screen transitions. Users will be able to log in and navigate to the home screen upon successful authentication.
4o