Building Your First React Native App with Expo: A Step-by-Step Guide
Building Your First React Native App with Expo: A Step-by-Step Guide
After setting up React Native with Expo, it's time to build your first functional app. In this guide, we'll explore the project structure and understand how different files contribute to the development workflow.
1. Understanding the Project Structure
When you create a new Expo project, you'll see a folder structure like this:
myApp/├── App.js
├── package.json
├── node_modules/
├── assets/
├── babel.config.js
└── app.json
Key Files and Their Purpose
- App.js – The main entry point of your app.
- package.json – Contains dependencies and project metadata.
- node_modules/ – Stores installed dependencies.
- assets/ – Holds static files like images and fonts.
- babel.config.js – Configures Babel for JavaScript transformations.
- app.json – Configures Expo-specific settings.
2. Creating Screens and Navigation
Most apps require multiple screens. You can use react-navigation
to navigate between screens.
Install React Navigation
npm install @react-navigation/native
npm install react-native-screens react-native-safe-area-context
npm install @react-navigation/stack
Set Up Navigation in App.js
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';
import DetailsScreen from './screens/DetailsScreen';
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
3. Creating Screens
Inside the screens/
directory, create HomeScreen.js
:
import { View, Text, Button } from 'react-native';
export default function HomeScreen({ navigation }) {
return (
<View>
<Text>Welcome to Home Screen</Text>
<Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
</View>
);
}
And DetailsScreen.js
:
import { View, Text } from 'react-native';
export default function DetailsScreen() {
return (
<View>
<Text>This is the Details Screen</Text>
</View>
);
}
4. Running Your App
Run your project with:
npx expo start
Scan the QR code in Expo Go to test your app with navigation.
Conclusion
Now you have a basic React Native app with multiple screens using Expo. In the next tutorial, we will explore state management and API integration. Happy coding! π
Comments
Post a Comment