Permalink
Find file Copy path
717 lines (490 sloc) 30.1 KB
id title
getting-started
Getting Started
<style> .toggler li { display: inline-block; position: relative; top: 1px; padding: 10px; margin: 0px 2px 0px 2px; border: 1px solid #05A5D1; border-bottom-color: transparent; border-radius: 3px 3px 0px 0px; color: #05A5D1; background-color: transparent; font-size: 0.99em; cursor: pointer; } .toggler li:first-child { margin-left: 0; } .toggler li:last-child { margin-right: 0; } .toggler ul { width: 100%; display: inline-block; list-style-type: none; margin: 0; border-bottom: 1px solid #05A5D1; cursor: default; } @media screen and (max-width: 960px) { .toggler li, .toggler li:first-child, .toggler li:last-child { display: block; border-bottom-color: #05A5D1; border-radius: 3px; margin: 2px 0px 2px 0px; } .toggler ul { border-bottom: 0; } } .toggler a { display: inline-block; padding: 10px 5px; margin: 2px; border: 1px solid #05A5D1; border-radius: 3px; text-decoration: none !important; } .display-guide-quickstart .toggler .button-quickstart, .display-guide-native .toggler .button-native, .display-os-mac .toggler .button-mac, .display-os-linux .toggler .button-linux, .display-os-windows .toggler .button-windows, .display-platform-ios .toggler .button-ios, .display-platform-android .toggler .button-android { background-color: #05A5D1; color: white; } block { display: none; } .display-guide-quickstart.display-platform-ios.display-os-mac .quickstart.ios.mac, .display-guide-quickstart.display-platform-ios.display-os-linux .quickstart.ios.linux, .display-guide-quickstart.display-platform-ios.display-os-windows .quickstart.ios.windows, .display-guide-quickstart.display-platform-android.display-os-mac .quickstart.android.mac, .display-guide-quickstart.display-platform-android.display-os-linux .quickstart.android.linux, .display-guide-quickstart.display-platform-android.display-os-windows .quickstart.android.windows, .display-guide-native.display-platform-ios.display-os-mac .native.ios.mac, .display-guide-native.display-platform-ios.display-os-linux .native.ios.linux, .display-guide-native.display-platform-ios.display-os-windows .native.ios.windows, .display-guide-native.display-platform-android.display-os-mac .native.android.mac, .display-guide-native.display-platform-android.display-os-linux .native.android.linux, .display-guide-native.display-platform-android.display-os-windows .native.android.windows { display: block; } </style>

This page will help you install and build your first React Native app. If you already have React Native installed, you can skip ahead to the Tutorial.

  • Quick Start
  • Building Projects with Native Code

Expo is the easiest way to start building a new React Native application. It allows you to start a project without installing or configuring any tools to build native code - no Xcode or Android Studio installation required (see Caveats).

Assuming that you have Node installed, you can use npm to install the Expo CLI command line utility:

npm install -g expo-cli

Then run the following commands to create a new React Native project called "AwesomeProject":

expo init AwesomeProject

cd AwesomeProject
npm start #you can also use: expo start

This will start a development server for you.

Running your React Native application

Install the Expo client app on your iOS or Android phone and connect to the same wireless network as your computer. On Android, use the Expo app to scan the QR code from your terminal to open your project. On iOS, follow on-screen instructions to get a link.

Modifying your app

Now that you have successfully run the app, let's modify it. Open App.js in your text editor of choice and edit some lines. The application should reload automatically once you save your changes.

That's it!

Congratulations! You've successfully run and modified your first React Native app.

Now what?

Expo also has docs you can reference if you have questions specific to the tool. You can also ask for help at Expo forums.

If you have a problem with Expo, before creating a new issue, please see if there's an existing issue about it:

If you're curious to learn more about React Native, continue on to the Tutorial.

Running your app on a simulator or virtual device

Expo CLI makes it really easy to run your React Native app on a physical device without setting up a development environment. If you want to run your app on the iOS Simulator or an Android Virtual Device, please refer to the instructions for building projects with native code to learn how to install Xcode and set up your Android development environment.

Once you've set these up, you can launch your app on an Android Virtual Device by running npm run android, or on the iOS Simulator by running npm run ios (macOS only).

Caveats

Because you don't build any native code when using Expo to create a project, it's not possible to include custom native modules beyond the React Native APIs and components that are available in the Expo client app.

If you know that you'll eventually need to include your own native code, Expo is still a good way to get started. In that case you'll just need to "eject" eventually to create your own native builds. If you do eject, the "Building Projects with Native Code" instructions will be required to continue working on your project.

Expo CLI configures your project to use the most recent React Native version that is supported by the Expo client app. The Expo client app usually gains support for a given React Native version about a week after the React Native version is released as stable. You can check this document to find out what versions are supported.

If you're integrating React Native into an existing project, you'll want to skip Expo CLI and go directly to setting up the native build environment. Select "Building Projects with Native Code" above for instructions on configuring a native build environment for React Native.

Follow these instructions if you need to build native code in your project. For example, if you are integrating React Native into an existing application, or if you "ejected" from Expo or Create React Native App, you'll need this section.

The instructions are a bit different depending on your development operating system, and whether you want to start developing for iOS or Android. If you want to develop for both iOS and Android, that's fine - you just have to pick one to start with, since the setup is a bit different.

Development OS: macOS Windows Linux Target OS: iOS Android

Unsupported

A Mac is required to build projects with native code for iOS. You can follow the Quick Start to learn how to build your app using Expo instead.

Installing dependencies

You will need Node, Watchman, the React Native command line interface, and Xcode.

While you can use any editor of your choice to develop your app, you will need to install Xcode in order to set up the necessary tooling to build your React Native app for iOS.

Installing dependencies

You will need Node, Watchman, the React Native command line interface, a JDK, and Android Studio.

Installing dependencies

You will need Node, the React Native command line interface, a JDK, and Android Studio.

Installing dependencies

You will need Node, the React Native command line interface, Python2, a JDK, and Android Studio.

While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.

Node, Watchman

We recommend installing Node and Watchman using Homebrew. Run the following commands in a Terminal after installing Homebrew:

brew install node
brew install watchman

If you have already installed Node on your system, make sure it is Node 8.3 or newer.

Watchman is a tool by Facebook for watching changes in the filesystem. It is highly recommended you install it for better performance.

Node

Follow the installation instructions for your Linux distribution to install Node 8.3 or newer.

Node, Python2, JDK

We recommend installing Node and Python2 via Chocolatey, a popular package manager for Windows.

React Native also requires a recent version of the Java SE Development Kit (JDK), as well as Python 2. Both can be installed using Chocolatey.

Open an Administrator Command Prompt (right click Command Prompt and select "Run as Administrator"), then run the following command:

choco install -y nodejs.install python2 jdk8

If you have already installed Node on your system, make sure it is Node 8.3 or newer. If you already have a JDK on your system, make sure it is version 8 or newer.

You can find additional installation options on Node's Downloads page.

The React Native CLI

Node comes with npm, which lets you install the React Native command line interface.

Run the following command in a Terminal:

npm install -g react-native-cli

If you get an error like Cannot find module 'npmlog', try installing npm directly: curl -0 -L https://npmjs.org/install.sh | sudo sh.

The React Native CLI

Node comes with npm, which lets you install the React Native command line interface.

Run the following command in a Command Prompt or shell:

npm install -g react-native-cli

If you get an error like Cannot find module 'npmlog', try installing npm directly: curl -0 -L https://npmjs.org/install.sh | sudo sh.

Xcode

The easiest way to install Xcode is via the Mac App Store. Installing Xcode will also install the iOS Simulator and all the necessary tools to build your iOS app.

If you have already installed Xcode on your system, make sure it is version 9.4 or newer.

Command Line Tools

You will also need to install the Xcode Command Line Tools. Open Xcode, then choose "Preferences..." from the Xcode menu. Go to the Locations panel and install the tools by selecting the most recent version in the Command Line Tools dropdown.

Xcode Command Line Tools

Java Development Kit

React Native requires a recent version of the Java SE Development Kit (JDK). Download and install Oracle JDK 8 if needed. You can also use OpenJDK 8 as an alternative.

Android development environment

Setting up your development environment can be somewhat tedious if you're new to Android development. If you're already familiar with Android development, there are a few things you may need to configure. In either case, please make sure to carefully follow the next few steps.

1. Install Android Studio

Download and install Android Studio. Choose a "Custom" setup when prompted to select an installation type. Make sure the boxes next to all of the following are checked:

  • Android SDK
  • Android SDK Platform
  • Performance (Intel ® HAXM) (See here for AMD)
  • Android Virtual Device
  • Android SDK
  • Android SDK Platform
  • Android Virtual Device

Then, click "Next" to install all of these components.

If the checkboxes are grayed out, you will have a chance to install these components later on.

Once setup has finalized and you're presented with the Welcome screen, proceed to the next step.

2. Install the Android SDK

Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 8.1 (Oreo) SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.

The SDK Manager can be accessed from the "Welcome to Android Studio" screen. Click on "Configure", then select "SDK Manager".

Android Studio Welcome

Android Studio Welcome

The SDK Manager can also be found within the Android Studio "Preferences" dialog, under Appearance & BehaviorSystem SettingsAndroid SDK.

Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 8.1 (Oreo) entry, then make sure the following items are checked:

  • Android SDK Platform 27
  • Intel x86 Atom_64 System Image or Google APIs Intel x86 Atom System Image

Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the "Android SDK Build-Tools" entry, then make sure that 27.0.3 is selected.

Finally, click "Apply" to download and install the Android SDK and related build tools.

3. Configure the ANDROID_HOME environment variable

The React Native tools require some environment variables to be set up in order to build apps with native code.

Add the following lines to your $HOME/.bash_profile config file:

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools

.bash_profile is specific to bash. If you're using another shell, you will need to edit the appropriate shell-specific config file.

Type source $HOME/.bash_profile to load the config into your current shell. Verify that ANDROID_HOME has been added to your path by running echo $PATH.

Please make sure you use the correct Android SDK path. You can find the actual location of the SDK in the Android Studio "Preferences" dialog, under Appearance & BehaviorSystem SettingsAndroid SDK.

Open the System pane under System and Security in the Windows Control Panel, then click on Change settings.... Open the Advanced tab and click on Environment Variables.... Click on New... to create a new ANDROID_HOME user variable that points to the path to your Android SDK:

ANDROID_HOME Environment Variable

The SDK is installed, by default, at the following location:

c:\Users\YOUR_USERNAME\AppData\Local\Android\Sdk

You can find the actual location of the SDK in the Android Studio "Preferences" dialog, under Appearance & BehaviorSystem SettingsAndroid SDK.

Open a new Command Prompt window to ensure the new environment variable is loaded before proceeding to the next step.

4. Add platform-tools to Path

Open the System pane under System and Security in the Windows Control Panel, then click on Change settings.... Open the Advanced tab and click on Environment Variables.... Select the Path variable, then click Edit. Click New and add the path to platform-tools to the list.

The default location for this folder is:

c:\Users\YOUR_USERNAME\AppData\Local\Android\Sdk\platform-tools

Watchman

Follow the Watchman installation guide to compile and install Watchman from source.

Watchman is a tool by Facebook for watching changes in the filesystem. It is highly recommended you install it for better performance and increased compatibility in certain edge cases (translation: you may be able to get by without installing this, but your mileage may vary; installing this now may save you from a headache later).

Creating a new application

Use the React Native command line interface to generate a new React Native project called "AwesomeProject":

react-native init AwesomeProject

This is not necessary if you are integrating React Native into an existing application, if you "ejected" from Expo (or Create React Native App), or if you're adding iOS support to an existing React Native project (see Platform Specific Code).

Creating a new application

Use the React Native command line interface to generate a new React Native project called "AwesomeProject":

react-native init AwesomeProject

This is not necessary if you are integrating React Native into an existing application, if you "ejected" from Create React Native App, or if you're adding Android support to an existing React Native project (see Platform Specific Code).

Preparing the Android device

You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.

Either way, you will need to prepare the device to run Android apps for development.

Using a physical device

If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.

Using a virtual device

If you use Android Studio to open ./AwesomeProject/android, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. Look for an icon that looks like this:

Android Studio AVD Manager

If you have just installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the Oreo API Level 27 image.

We recommend configuring VM acceleration on your system to improve performance. Once you've followed those instructions, go back to the AVD Manager.

If you don't have HAXM installed, click on "Install HAXM" or follow these instructions to set it up, then go back to the AVD Manager.

If you don't have HAXM installed, follow these instructions to set it up, then go back to the AVD Manager.

Click "Next" then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it, then proceed to the next step.

Running your React Native application

Run react-native run-ios inside your React Native project folder:

cd AwesomeProject
react-native run-ios

You should see your new app running in the iOS Simulator shortly.

AwesomeProject on iOS

react-native run-ios is just one way to run your app. You can also run it directly from within Xcode or Nuclide.

If you can't get this to work, see the Troubleshooting page.

Running on a device

The above command will automatically run your app on the iOS Simulator by default. If you want to run the app on an actual physical iOS device, please follow the instructions here.

Running your React Native application

Run react-native run-android inside your React Native project folder:

cd AwesomeProject
react-native run-android

If everything is set up correctly, you should see your new app running in your Android emulator shortly.

AwesomeProject on Android

AwesomeProject on Android

react-native run-android is just one way to run your app - you can also run it directly from within Android Studio or Nuclide.

If you can't get this to work, see the Troubleshooting page.

Modifying your app

Now that you have successfully run the app, let's modify it.

  • Open App.js in your text editor of choice and edit some lines.
  • Hit ⌘R in your iOS Simulator to reload the app and see your changes!
  • Open App.js in your text editor of choice and edit some lines.
  • Press the R key twice or select Reload from the Developer Menu (⌘M) to see your changes!

Modifying your app

Now that you have successfully run the app, let's modify it.

  • Open App.js in your text editor of choice and edit some lines.
  • Press the R key twice or select Reload from the Developer Menu (Ctrl + M) to see your changes!

That's it!

Congratulations! You've successfully run and modified your first React Native app.

That's it!

Congratulations! You've successfully run and modified your first React Native app.

Now what?

  • Turn on Live Reload in the Developer Menu. Your app will now reload automatically whenever you save any changes!

  • If you want to add this new React Native code to an existing application, check out the Integration guide.

If you're curious to learn more about React Native, continue on to the Tutorial.

Now what?

  • Turn on Live Reload in the Developer Menu. Your app will now reload automatically whenever you save any changes!

  • If you want to add this new React Native code to an existing application, check out the Integration guide.

If you're curious to learn more about React Native, continue on to the Tutorial.

<script> function displayTab(type, value) { var container = document.getElementsByTagName('block')[0].parentNode; container.className = 'display-' + type + '-' + value + ' ' + container.className.replace(RegExp('display-' + type + '-[a-z]+ ?'), ''); } function convertBlocks() { // Convert
......
// Into
......
var blocks = document.querySelectorAll('block'); for (var i = 0; i < blocks.length; ++i) { var block = blocks[i]; var span = blocks[i].parentNode; var container = span.parentNode; container.insertBefore(block, span); container.removeChild(span); } // Convert
...content...
// Into
...content...
blocks = document.querySelectorAll('block'); for (var i = 0; i < blocks.length; ++i) { var block = blocks[i]; while ( block.nextSibling && block.nextSibling.tagName !== 'BLOCK' ) { block.appendChild(block.nextSibling); } } } function guessPlatformAndOS() { if (!document.querySelector('block')) { return; } // If we are coming to the page with a hash in it (i.e. from a search, for example), try to get // us as close as possible to the correct platform and dev os using the hashtag and block walk up. var foundHash = false; if ( window.location.hash !== '' && window.location.hash !== 'content' ) { // content is default var hashLinks = document.querySelectorAll( 'a.hash-link' ); for ( var i = 0; i < hashLinks.length && !foundHash; ++i ) { if (hashLinks[i].hash === window.location.hash) { var parent = hashLinks[i].parentElement; while (parent) { if (parent.tagName === 'BLOCK') { // Could be more than one target os and dev platform, but just choose some sort of order // of priority here. // Dev OS if (parent.className.indexOf('mac') > -1) { displayTab('os', 'mac'); foundHash = true; } else if ( parent.className.indexOf('linux') > -1 ) { displayTab('os', 'linux'); foundHash = true; } else if ( parent.className.indexOf('windows') > -1 ) { displayTab('os', 'windows'); foundHash = true; } else { break; } // Target Platform if (parent.className.indexOf('ios') > -1) { displayTab('platform', 'ios'); foundHash = true; } else if ( parent.className.indexOf('android') > -1 ) { displayTab('platform', 'android'); foundHash = true; } else { break; } // Guide if (parent.className.indexOf('native') > -1) { displayTab('guide', 'native'); foundHash = true; } else if ( parent.className.indexOf('quickstart') > -1 ) { displayTab('guide', 'quickstart'); foundHash = true; } else { break; } break; } parent = parent.parentElement; } } } } // Do the default if there is no matching hash if (!foundHash) { var isMac = navigator.platform === 'MacIntel'; var isWindows = navigator.platform === 'Win32'; displayTab('platform', isMac ? 'ios' : 'android'); displayTab( 'os', isMac ? 'mac' : isWindows ? 'windows' : 'linux' ); displayTab('guide', 'quickstart'); displayTab('language', 'objc'); } } convertBlocks(); guessPlatformAndOS(); </script>