Learn React Native Β· 1/15

Introduction & Setup: Your Mobile Journey Begins

10 min read

Introduction & Setup: Your Mobile Journey Begins πŸš€

Welcome, future mobile developer! You're about to embark on a thrilling journey into the world of React Native. But before we start slinging code and building beautiful apps, let's get our bearings and understand what we're getting into. This guide will be your friendly companion, taking you from "Why should I even care?" to "Look, Ma, I built an app!".

The "Why" Before the "How": Why React Native?

So, you want to build a mobile app. Awesome! But you've probably heard of a bunch of different ways to do it. You could learn Swift or Objective-C to build for iPhones, or Java or Kotlin for Android devices. The problem? That's like learning two completely different languages to say the same thing. πŸ˜…

The Problem: Building separate apps for iOS and Android is time-consuming and expensive. You have to write and maintain two different sets of code, which can be a real headache.

The Solution: React Native!

React Native is a JavaScript framework for building real, native mobile apps for both iOS and Android from a single codebase. It's like having a universal translator for your code. You write your app's logic once using JavaScript, and React Native handles the rest, giving you a truly native user interface on both platforms.

Here's why developers and companies are flocking to React Native:

  • Code Reusability: Write your code once and deploy it on both Android and iOS. This can save a massive amount of time and resources. Imagine building a house and being able to use the same blueprint for a second, slightly different house. That's the power of code reuse.
  • Faster Development: Features like "Hot Reloading" allow you to see your code changes in real-time, without having to recompile your entire app. This makes the development process incredibly fast and iterative. It’s like being able to see your painting change instantly with every brushstroke, rather than waiting for the paint to dry.
  • Native Performance: Your React Native app will have a user interface built with the same fundamental building blocks as native iOS and Android apps. This means you get the smooth performance and feel of a native app.
  • A Massive Community: React Native is an open-source project with a huge and active community. This means you'll find tons of libraries, tutorials, and support to help you on your journey.

Fun Fact: Facebook (now Meta) created React Native and uses it in many of their own apps, including Facebook, Instagram, and Messenger. So, you're in good company!

Expo vs. React Native CLI: Choosing Your Path

Alright, you're sold on React Native. Now you have your first big decision to make: how are you going to build your app? You have two main paths to choose from: Expo and React Native CLI. Think of it as choosing between a guided tour and an off-road adventure. Both will get you to your destination, but the journey will be very different.

What is Expo? πŸ€”

Expo is a framework and a platform for universal React applications. It's a set of tools and services built around React Native that makes it much easier and faster to get started with building your app.

Imagine you're building with LEGOs. Expo is like getting a pre-packaged LEGO set with detailed instructions and all the specific pieces you need to build a cool car. You don't have to worry about finding the right wheels or figuring out how the engine goes together; it's all there for you.

Key Features of Expo:

  • Quick Start: You can get a new project up and running in minutes with a single command.
  • Expo Go App: This is a magical app you can install on your phone that lets you see and test your app in real-time without needing to set up complex development environments.
  • Pre-built APIs: Expo provides a rich set of pre-built APIs for accessing device features like the camera, push notifications, and sensors.
  • Over-the-Air (OTA) Updates: You can push updates to your app instantly without going through the app store review process.

What is React Native CLI? πŸ§‘β€πŸ’»

React Native CLI (Command Line Interface) is the "barebones" way to build a React Native app. It gives you more control and flexibility, but also requires more setup and configuration.

Back to our LEGO analogy. Using the React Native CLI is like having a giant bin of assorted LEGO bricks. You have the freedom to build anything you can imagine, but you're also responsible for figuring out how all the pieces fit together.

Key Features of React Native CLI:

  • Full Native Control: You have direct access to the native iOS and Android project files, allowing you to integrate any native module or write your own custom native code.
  • Smaller App Size: Because you only include the native modules you actually need, your final app can be smaller in size.
  • More Flexibility: You are not limited by the APIs and modules provided by the Expo SDK.

So, Which One Should You Choose?

For beginners, the official React Native documentation strongly recommends starting with Expo. It abstracts away a lot of the complexity of native development, allowing you to focus on learning the fundamentals of React Native.

Here's a simple breakdown to help you decide:

Feature Expo React Native CLI
Ease of Use 🟒 Very easy, great for beginners. 🟑 Steeper learning curve, requires more setup.
Development Speed πŸš€ Super fast to get started and iterate. πŸƒβ€β™‚οΈ Can be slower due to manual configuration.
Access to Native Code 🟠 Limited, but improving with custom development clients. 🟒 Full access to native projects.
App Size 🟠 Can be larger due to including the entire Expo SDK. 🟒 Generally smaller as you only include what you need.
Best For Beginners, prototypes, and apps that don't require custom native modules. Experienced developers, complex apps, and projects that require deep native integration.

Our recommendation for this course: We'll be using Expo for its simplicity and fast setup. It's the perfect way to get your feet wet without getting bogged down in configuration details.

Setting Up Your Development Environment & Emulators

Time to get your hands dirty! Let's set up everything you need to start building your first React Native app with Expo.

Step 1: Install Node.js and npm

React Native and Expo are built on top of Node.js, a JavaScript runtime. You'll also need npm (Node Package Manager), which comes with Node.js, to install and manage your project's dependencies.

  1. Download and Install Node.js: Head over to the official Node.js website and download the LTS (Long Term Support) version. This is the most stable version and is recommended for most users.
  2. Verify the Installation: Open your terminal or command prompt and run the following commands to make sure everything is installed correctly:
    bash
    node -v
    npm -v
    You should see the version numbers of Node.js and npm printed to the console.

Step 2: Install the Expo CLI

The Expo CLI is a command-line tool that will help you create, manage, and run your Expo projects.

  1. Install the Expo CLI globally: Open your terminal and run the following command:
    bash
    npm install -g expo-cli
  2. Verify the Installation: To make sure the Expo CLI was installed correctly, run:
    bash
    expo --version
    This should display the version of the Expo CLI.

Step 3: Install the Expo Go App on Your Phone

The easiest way to see your app in action is to use the Expo Go app on your physical iOS or Android device.

  1. Download Expo Go: Go to the App Store (for iOS) or the Google Play Store (for Android) and search for "Expo Go". Download and install the app.

(Optional but Recommended) Step 4: Setting Up an Emulator

While using a physical device is great, sometimes it's more convenient to use an emulator (for Android) or a simulator (for iOS) that runs on your computer.

For Android (Android Studio)

  1. Download and Install Android Studio: Go to the official Android Studio website and download the latest version.
  2. Install the Android SDK: During the installation process, make sure to select the "Android SDK," "Android SDK Platform," and "Android Virtual Device" options. React Native requires at least the Android 15 (VanillaIceCream) SDK.
  3. Configure Environment Variables (for Windows): You'll need to set up the ANDROID_HOME environment variable.
  4. Create a Virtual Device: Open Android Studio, go to "Tools" -> "Device Manager," and click on "Create Device" to set up a new Android Virtual Device (AVD).

For iOS (Xcode - macOS only)

  1. Install Xcode: If you're on a Mac, you can install Xcode from the Mac App Store. Xcode comes with the iOS simulator, which you can use to run your app.
  2. Install Command Line Tools: Open Xcode, go to "Preferences" -> "Locations," and make sure the "Command Line Tools" are selected.

Your First React Native App: "Hello World"

You've made it through the setup! πŸŽ‰ Now for the fun part: creating your very first React Native app.

Step 1: Create a New Expo Project

  1. Open your terminal and navigate to the directory where you want to create your project.
  2. Run the following command to create a new project. Replace MyFirstApp with whatever you want to name your app.
    bash
    npx create-expo-app MyFirstApp
    This will create a new directory with your app's name and install all the necessary dependencies.

Step 2: Navigate into Your Project Directory

Once the project is created, move into the new directory:

bash
cd MyFirstApp

Step 3: Start the Development Server

Now, it's time to fire up the development server:

bash
npx expo start

This command will start the Metro Bundler, which is a JavaScript bundler for React Native. You'll see a QR code in your terminal and a new browser tab will open with the Expo Dev Tools.

Step 4: Run the App

You have a few options for running your app:

  • On your physical device: Open the Expo Go app on your phone and scan the QR code from your terminal. Your app will magically appear on your phone!
  • On an Android emulator: With your Android emulator running, press a in the terminal where the development server is running.
  • On an iOS simulator (macOS only): With your iOS simulator running, press i in the terminal.

Step 5: Say "Hello World!"

Now that your app is running, let's make it say "Hello World!".

  1. Open the project in your favorite code editor. A popular choice is Visual Studio Code.
  2. Find and open the App.js file. This is the main entry point of your application.
  3. Replace the existing code with the following:
javascript
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Hello World!</Text>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

What's happening in this code?

  • We're importing the necessary components from React Native: View (a container that supports layout with flexbox), Text (for displaying text), and StyleSheet (for creating styles).
  • The App function is our main component. It returns a View that contains a Text component.
  • We're using the StyleSheet API to create some basic styles for our container to center the text on the screen.

Save the App.js file. Thanks to Hot Reloading, you should see your app update instantly on your device or emulator with the "Hello World!" message.

Congratulations! You've just built and run your very first React Native application. You're now officially a mobile app developer! πŸ₯³


πŸ“š Extra Reading & References

Challenge

You are tasked with modifying a basic React Native application's main component. Your goal is to structure and style the App.js file to display a structured and styled message.

Requirements:

  1. Inside the main View component, replace the existing Text element with two new Text components.
  2. The first Text component should act as a title and display the text: "Welcome to My App".
  3. The second Text component should act as a subtitle and display the text: "Built with React Native".
  4. Use the StyleSheet.create method to define all styles for your components.
  5. The main container View must be styled to take up the full screen and center its children both horizontally and vertically. It should also have a background color of #f5f5f5.
  6. The title Text component must have a font size of 24 and a bold font weight.
  7. The subtitle Text component must have a font size of 16 and an italic font style.

Expected result

Your code should be a single valid React Native component defined in App.js.

Code Structure:

  • The default export must be a function component named App.
  • The App component's JSX should return a single parent View component.
  • Inside the parent View, there should be two Text components as direct children.
  • A StyleSheet object named styles must be created using StyleSheet.create.

Styling Requirements (styles object):

  • There should be a style key named container with the following properties:
    • flex: 1
    • justifyContent: 'center'
    • alignItems: 'center'
    • backgroundColor: '#f5f5f5'
  • There should be a style key for the title (e.g., title) with the following properties:
    • fontSize: 24
    • fontWeight: 'bold'
  • There should be a style key for the subtitle (e.g., subtitle) with the following properties:
    • fontSize: 16
    • fontStyle: 'italic'

Component and Prop application:

  • The parent View must have its style prop assigned to styles.container.
  • The first Text component must display "Welcome to My App" and have its style prop assigned to the title style you created.
  • The second Text component must display "Built with React Native" and have its style prop assigned to the subtitle style you created.
javascript

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.