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

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

ComponentDescription
ViewLike a <div>
TextLike a <p> or <span>
ImageUsed to render images
TextInputUser input
ButtonA simple button
ScrollViewScrollable container
FlatListOptimized list rendering
TouchableOpacity, PressableTouchable 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={&#96;Count: &#36;{count}&#96;} 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)

MethodDescription
componentDidMountCalled after mount
componentDidUpdateCalled on update
componentWillUnmountCleanup 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

TypeScript with React Native

npx react-native init MyTSApp --template react-native-template-typescript

Push Notifications

Use:

Authentication

Use:

Deployment

Android:

cd android ./gradlew assembleRelease

iOS:

Expo:

npx expo build:android npx expo build:ios

Best Practices

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

Summary Roadmap

StageTopics
BeginnerSetup, Components, Props, State, Styling, Navigation
IntermediateAPI Calls, AsyncStorage, Lists, Hooks, Forms, Custom Components
AdvancedRedux, TypeScript, Authentication, Push Notifications, Native Modules
DeploymentBuild & Release on Play Store and App Store