React Native
This React Native cheatsheet is a concise
React Native is a framework developed by Facebook to build native mobile apps using JavaScript and React. Instead of building apps using Swift for iOS or Java/Kotlin for Android, you can use React Native to build cross-platform apps with one codebase.
Prerequisites
- Familiarity with JavaScript and React.js
- Basic knowledge of Node.js and npm/yarn
- Android Studio or Xcode installed for emulators
- Optional: Physical Android or iOS device
Setup and Installation
1. Install Node.js, npm
Download and install Node.js
node -v npm -v
2. Install React Native CLI (using Expo OR Bare CLI)
Option 1: Expo CLI (easier, good for beginners)
npm install -g expo-cli expo init MyApp cd MyApp expo start
Option 2: React Native CLI (more control, good for advanced apps)
npx react-native init MyApp cd MyApp npx react-native run-android # For Android npx react-native run-ios # For iOS (Mac only)
Project Structure
MyApp/
├── android/ # Android-specific code
├── ios/ # iOS-specific code
├── node_modules/
├── App.js # Root component
├── package.json
└── ...
React Native Core Concepts
1. Components
Functional Component
const Welcome = () => <Text>Hello World</Text>;
Class Component
class Welcome extends React.Component { render() { return <Text>Hello World</Text>; } }
2. Styling
React Native uses StyleSheet instead of CSS.
import { StyleSheet, Text, View } from 'react-native'; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
3. Core Components
| Component | Description |
|---|---|
| View | Like a <div> |
| Text | Like a <p> or <span> |
| Image | Used to render images |
| TextInput | User input |
| Button | A simple button |
| ScrollView | Scrollable container |
| FlatList | Optimized list rendering |
| TouchableOpacity, Pressable | Touchable components |
4. Props and State
Props
const Greeting = ({ name }) => <Text>Hello {name}</Text>;
State (Hooks)
const Counter = () => { const [count, setCount] = useState(0); return ( <Button title={`Count: ${count}`} onPress={() => setCount(count + 1)} /> ); };
5. Navigation
Install React Navigation:
npm install @react-navigation/native npx expo install react-native-screens react-native-safe-area-context react-native-gesture-handler npm install @react-navigation/native-stack
Example Stack Navigator
import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; const Stack = createNativeStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Lifecycle Methods (For Class Components)
| Method | Description |
|---|---|
| componentDidMount | Called after mount |
| componentDidUpdate | Called on update |
| componentWillUnmount | Cleanup before unmount |
Async Storage (Local Storage)
npm install @react-native-async-storage/async-storage
import AsyncStorage from '@react-native-async-storage/async-storage'; await AsyncStorage.setItem('key', 'value'); const value = await AsyncStorage.getItem('key');
API Calls (Using fetch or Axios)
useEffect(() => { fetch('https://api.example.com/data') .then(res => res.json()) .then(data => setData(data)); }, []);
Device Features
Install required packages using Expo:
npx expo install expo-camera expo-location expo-permissions expo-image-picker
You can now use camera, GPS, image-picker, etc.
Advanced Topics
Redux (Global State Management)
npm install redux react-redux
- Create store
- Use reducers
- Dispatch actions
- Connect components
TypeScript with React Native
npx react-native init MyTSApp --template react-native-template-typescript
Push Notifications
Use:
- Firebase Cloud Messaging (FCM)
- Expo Notifications
Authentication
Use:
- Firebase Authentication
- OAuth (Google, Facebook, Apple)
- AuthContext + AsyncStorage
Deployment
Android:
- Generate signed APK:
cd android ./gradlew assembleRelease
iOS:
- Use Xcode to archive and upload to App Store
Expo:
npx expo build:android npx expo build:ios
Best Practices
- Use functional components and hooks
- Modularize code (components/screens/services)
- Handle errors and loading states
- Use responsive designs (Dimensions, Flexbox)
- Keep dependencies updated
Testing
Install Jest:
npm install --save-dev jest @testing-library/react-native
Write unit tests for components.
Folder Structure (Recommended)
/src
├── /components
├── /screens
├── /navigation
├── /assets
├── /services
└── App.js
Resources to Learn More
- React Native Docs
- Expo Docs
- React Navigation Docs
- YouTube Channels: CodeWithMosh, Net Ninja, Programming with Mosh
Summary Roadmap
| Stage | Topics |
|---|---|
| Beginner | Setup, Components, Props, State, Styling, Navigation |
| Intermediate | API Calls, AsyncStorage, Lists, Hooks, Forms, Custom Components |
| Advanced | Redux, TypeScript, Authentication, Push Notifications, Native Modules |
| Deployment | Build & Release on Play Store and App Store |