react native mobile

Setting Up Your Development Environment

In this chapter, we'll set up everything you need to start developing React Native applications.

Node.js and React Native CLI

First, let's install the necessary tools:

# Check Node.js version
node --version

# Install React Native CLI
npm install -g react-native-cli

# Verify installation
react-native --version

Platform-Specific Setup

iOS Development (macOS only)

  1. Install Xcode from the App Store
  2. Install Command Line Tools:
xcode-select --install
  1. Install CocoaPods:
sudo gem install cocoapods

Android Development

  1. Install Android Studio
  2. Set up Android SDK:
# Add to ~/.bash_profile or ~/.zshrc
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

Creating Your First Project

# Create a new React Native project
npx react-native init MyFirstApp

# Navigate to project directory
cd MyFirstApp

# Install dependencies
npm install

# Start Metro bundler
npm start

Project Structure

MyFirstApp/
├── android/          # Android-specific code
├── ios/             # iOS-specific code
├── node_modules/    # Dependencies
├── src/             # Source code
│   ├── components/  # Reusable components
│   ├── screens/     # Screen components
│   ├── navigation/  # Navigation configuration
│   └── assets/      # Images, fonts, etc.
├── App.js           # Root component
└── package.json     # Project configuration

Running Your App

iOS

# Start iOS simulator
npx react-native run-ios

# Specify simulator
npx react-native run-ios --simulator="iPhone 14"

Android

# Start Android emulator
npx react-native run-android

Development Tools

React Native Debugger

# Install React Native Debugger (macOS)
brew install --cask react-native-debugger

# Start debugger
open "rndebugger://set-debugger-loc?host=localhost&port=8081"

Metro Bundler

Metro is React Native's JavaScript bundler:

# Start Metro
npm start

# Clear Metro cache
npm start -- --reset-cache

Troubleshooting Common Issues

iOS Build Issues

cd ios
pod install
cd ..

Android Build Issues

# Clean Android build
cd android
./gradlew clean
cd ..

Best Practices

  1. Project Organization
// components/Button.js
import React from 'react';
import { TouchableOpacity, Text, StyleSheet } from 'react-native';

export const Button = ({ title, onPress }) => (
  <TouchableOpacity style={styles.button} onPress={onPress}>
    <Text style={styles.text}>{title}</Text>
  </TouchableOpacity>
);

const styles = StyleSheet.create({
  button: {
    padding: 12,
    backgroundColor: '#007AFF',
    borderRadius: 8,
  },
  text: {
    color: '#FFFFFF',
    fontSize: 16,
    textAlign: 'center',
  },
});
  1. File Naming Conventions
  • Components: PascalCase
  • Utilities: camelCase
  • Styles: styles.js

In the next chapter, we'll start building our first React Native application!