No history yet

Introduction to React Native

Build Mobile Apps with Web Skills

Imagine you've built a fantastic website using React. Now, you want to create a mobile app for both iPhones and Android devices. Traditionally, this meant starting over. You'd need to learn Swift for iOS and Kotlin for Android, effectively building two separate apps from scratch. That's a lot of work.

React Native changes the game. It’s a framework created by Meta that lets you build native mobile apps using JavaScript and React. Instead of writing two different codebases, you write one. React Native acts as a bridge, translating your JavaScript code into the native user interface elements of iOS and Android.

This means your app won't just look like a website stuck inside a mobile shell. It will use the actual, native buttons, text inputs, and scroll views that users are accustomed to on their devices, providing a smooth and authentic experience.

Cross-platform development tools like React Native and Flutter offer a flexible approach, allowing you to write code once and deploy it on both Android and iOS.

Setting Up Your Environment

Before diving in, you'll need a solid understanding of JavaScript and the core concepts of React, like components, props, and state. If you have that foundation, you're ready to set up your development environment.

React Native relies on a few essential tools to work its magic. We'll use Homebrew, a package manager for macOS, to install them. If you don't have Homebrew, you can install it by running the command on their official website. Once Homebrew is ready, you can install the dependencies.

# Install Node.js and Watchman
brew install node
brew install watchman

Node.js is a JavaScript runtime that lets you run JavaScript outside a web browser, and Watchman is a tool by Meta for watching for file changes. Next, you need the React Native Command Line Interface (CLI). This tool helps you create and manage your projects. You’ll also need Xcode, which you can install from the Mac App Store. It includes the iOS Simulator and all the required tools for building your app.

For this guide, we are focusing on the iOS setup. Be sure to open Xcode after installing it to accept the license agreement and let it install its command line tools.

Your First React Native App

With the environment set up, you can create your first project. The React Native CLI makes this simple. Open your terminal, navigate to where you want to store your project, and run the following command:

npx react-native@latest init MyFirstApp

This command creates a new directory called MyFirstApp with all the necessary files and configurations. Let's look at the structure.

Folder / FilePurpose
node_modules/Contains all the JavaScript dependencies for the project.
ios/Contains the native Xcode project for the iOS version of your app.
android/Contains the native project files for the Android version of your app.
App.tsxThe main entry point for your app's user interface. This is where you'll write most of your React code.
index.jsThe starting point of the application that registers the main component.
package.jsonLists project dependencies and scripts.

You'll spend most of your time in .tsx or .jsx files, just like in web React development. The ios and android folders are for native configuration, which you won't need to touch often at the beginning.

Now for the exciting part: running the app. Navigate into your new project directory and run the command to start the iOS simulator.

# Change directory into your project
cd MyFirstApp

# Run the app on the iOS simulator
npx react-native run-ios

This command does a few things. It starts Metro, the JavaScript bundler that compiles your code. Then, it builds the native iOS app inside the ios folder and launches it on an iOS Simulator. After a moment, you should see a welcome screen for your new React Native application running on a virtual iPhone.

Lesson image

You've successfully set up your environment, created a new project, and launched it. You're now ready to start replacing the template code with your own components and build something amazing.