Introduction to React Native Development
Getting started with React Native for cross-platform mobile development and building your first app.
React Native enables building beautiful, native mobile apps from a single codebase using React. This comprehensive introduction covers everything you need to start your React Native journey.
Getting Started
Install React Native and set up your development environment:
# Install Node.js and npm
# Then install React Native CLI
npm install -g react-native-cli
# Create a new project
npx react-native init MyApp
cd MyApp
# Run on iOS
npx react-native run-ios
# Run on Android
npx react-native run-androidUnderstanding React Native
React Native uses JavaScript/TypeScript and React: - Component-based architecture - JSX syntax - React hooks and state management - Native performance
Component System
Everything in React Native is a component:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const App = () => {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello React Native!</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
},
});
export default App;State Management
Choose a state management solution: - useState - For simple local state - Context API - For app-wide state - Redux - For complex state logic - Zustand - Lightweight alternative
Navigation
Navigate between screens using React Navigation:
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}API Integration
Make HTTP requests:
import { useEffect, useState } from 'react';
const fetchData = async () => {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
};Local Storage
Store data locally using AsyncStorage:
import AsyncStorage from '@react-native-async-storage/async-storage';
// Save
await AsyncStorage.setItem('key', 'value');
// Read
const value = await AsyncStorage.getItem('key');Fast Refresh
React Native's Fast Refresh lets you see changes instantly without losing app state. Changes are automatically reflected in your app.
Deployment
Build for production:
# Android
cd android
./gradlew assembleRelease
# iOS
cd ios
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp archiveStart building cross-platform apps today.