How to Build a Mobile App with React Native: A Step-by-Step Tutorial
React Native lets you build cross-platform mobile apps using JavaScript and React. Instead of writing separate code for iOS and Android, you share most of your logic while still delivering native-like performance. This tutorial walks you through the key steps to get your first app up and running.
Before you start, make sure you have Node.js (v18 or later), a code editor like VS Code, and either Expo CLI (for easier setup) or React Native CLI (for more control). This guide uses Expo, which handles most configuration for you.
1. Set Up Your Development Environment
Install Expo CLI globally:
npm install -g expo-cli- Create a new project:
expo init MyFirstAppand choose the blank template. - Navigate into the folder:
cd MyFirstAppand start the dev server:npm start.
You’ll see a QR code. Install the Expo Go app on your phone and scan it to preview your app live.
2. Build Your First Screen
Open App.js. Replace its content with a simple “Hello World” component:
import { Text, View } from 'react-native';
export default function App() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Hello, React Native!</Text>
</View>
);
}
Your phone should show the text instantly. React Native uses View (like a div) and Text (like a span) instead of HTML elements.
3. Add Navigation Between Screens
Most apps need multiple screens. Install React Navigation:
npm install @react-navigation/native @react-navigation/stack- Wrap your app in
NavigationContainerand define a stack navigator with two screens (e.g., Home and Details).
Example minimal setup:
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
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>
);
}
4. Handle State and API Calls
Use React’s useState and useEffect to manage data. For example, fetch data from a public API inside a screen.
- Call
fetch('https://api.example.com/data')insideuseEffect. - Store the result in state and display it in a
FlatListfor efficient scrolling.
Conclusion
With these steps – setup, first screen, navigation, and data handling – you’ve built the foundation of a React Native app. Expand by adding user input, local storage with AsyncStorage, or push notifications. The same codebase will run on both iOS and Android, saving you hours of duplicate work.