react-native/docs/GettingStarted.md

462 lines
18 KiB
Markdown
Raw Normal View History

---
id: quick-start-getting-started
title: Getting Started
layout: docs
category: The Basics
permalink: docs/getting-started.html
next: tutorial
---
Welcome to React Native! This page will help you install React Native on
your system, so that you can build apps with it right away. If you already
have React Native installed, you can skip ahead to the
[Tutorial](/react-native/docs/tutorial.html).
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.
<div class="toggler">
<style>
.toggler a {
display: inline-block;
padding: 10px 5px;
margin: 2px;
border: 1px solid #05A5D1;
border-radius: 3px;
text-decoration: none !important;
}
.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-platform-ios.display-os-mac .ios.mac,
.display-platform-ios.display-os-linux .ios.linux,
.display-platform-ios.display-os-windows .ios.windows,
.display-platform-android.display-os-mac .android.mac,
.display-platform-android.display-os-linux .android.linux,
.display-platform-android.display-os-windows .android.windows {
display: block;
}</style>
<span>Mobile OS:</span>
<a href="javascript:void(0);" class="button-ios" onclick="display('platform', 'ios')">iOS</a>
<a href="javascript:void(0);" class="button-android" onclick="display('platform', 'android')">Android</a>
<span>Development OS:</span>
<a href="javascript:void(0);" class="button-mac" onclick="display('os', 'mac')">Mac</a>
<a href="javascript:void(0);" class="button-linux" onclick="display('os', 'linux')">Linux</a>
<a href="javascript:void(0);" class="button-windows" onclick="display('os', 'windows')">Windows</a>
</div>
<!-- ######### LINUX AND WINDOWS for iOS ##################### -->
<block class="linux windows ios" />
## Unsupported
<div>Unfortunately, Apple only lets you develop for iOS on a Mac. If you want to build an iOS app but you don't have a Mac yet, you can try starting with the <a href="" onclick="display('platform', 'android')">Android</a> instructions instead.</div>
<center><img src="img/react-native-sorry-not-supported.png" width="150"></img></center>
<!-- ######### MAC for iOS ##################### -->
<block class="mac ios android" />
## Installing Dependencies
<block class="mac ios" />
You will need Node.js, Watchman, the React Native command line interface, and Xcode.
<block class="mac android" />
You will need Node.js, Watchman, the React Native command line interface, and Android Studio.
<block class="mac ios android" />
### Node, Watchman
We recommend installing Node and Watchman using [Homebrew](http://brew.sh/). Run the following commands in a Terminal after installing Homebrew:
```
brew install node
brew install watchman
```
> [Watchman](https://facebook.github.io/watchman) is a tool by Facebook for watching
changes in the filesystem. It is highly recommended you install it for better performance.
### The React Native CLI
Node.js 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 a *permission error*, try using sudo: `sudo npm install -g react-native-cli`.
> If you get an error like `Cannot find module 'npmlog'`, try installing npm directly: `curl -0 -L http://npmjs.org/install.sh | sudo sh`.
<block class="mac ios" />
### Xcode
The easiest way to install Xcode is via the [Mac App Store](https://itunes.apple.com/us/app/xcode/id497799835?mt=12). Installing Xcode will also install the iOS Simulator and all the necessary tools to build your iOS app.
<block class="mac android" />
### 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](https://developer.android.com/studio/install.html).
#### 2. Confirm the Android SDK is installed
Android Studio installs `Android 7.0 (Nougat)` by default. You can confirm that the SDK was installed by clicking on "Configure" in the last screen in the Android Studio Setup Wizard, or by opening "Preferences" from the Android Studio menu, then choosing `Appearance and Behavior``System Settings``Android SDK`.
![Android Studio SDK Manager](img/react-native-android-studio-configure-sdk.png)
Select "SDK Platforms" from within the SDK Manager and you should see a blue checkmark next to "Android 7.0 (Nougat)". In case it is not, click on the checkbox and then "Apply".
![Android Studio SDK Manager](img/react-native-sdk-platforms.png)
> If you wish to support older versions of Android, you can install additional Android SDKs from this screen.
#### 3. Set up paths
The React Native command line interface requires the `ANDROID_HOME` environment variable to be set up. You can configure it in a Terminal using the following command:
```
export ANDROID_HOME=~/Library/Android/sdk
```
To avoid doing this every time you open a new Terminal, create (or edit) `~/.bashrc` using your favorite text editor and add the following lines:
```
export ANDROID_HOME=~/Library/Android/sdk
export PATH=${PATH}:${ANDROID_HOME}/tools
```
The second line will add the `android` tool to your path, which will come in handy in the next step.
> Please make sure you export the correct path for `ANDROID_HOME` if you did not install the Android SDK using Android Studio. If you install the Android SDK using Homebrew, it will be located at `/usr/local/opt/android-sdk`.
#### 4. Set up your Android Virtual Device
Android Studio should have set up an Android Virtual Device for you during installation, but it is very common to run into an issue where Android Studio fails to install the AVD.
![Android Studio AVD Manager](img/react-native-tools-avd.png)
To see the list of available AVDs, launch the "AVD Manager" from within Android Studio or run the following command in a Terminal:
```
android avd
```
You may follow the [Android Studio User Guide](https://developer.android.com/studio/run/managing-avds.html) to create a new AVD if needed.
> If you see "No system images installed for this target." under CPU/ABI, go back to your "SDK Manager" and click on "Show Package Details" under "SDK Platforms". You will then be able to install any missing system images, such as "Google APIs Intel Atom (x86)".
<!-- ######### LINUX and WINDOWS for ANDROID ##################### -->
<block class="windows linux android" />
## Installing Dependencies
<block class="linux android" />
You will need Node.js, the React Native command line interface, and Android Studio.
### Node
Follow the [installation instructions for your Linux distribution](https://nodejs.org/en/download/package-manager/) to install Node.js 4 or newer.
<block class='windows android' />
You will need Node.js, the React Native command line interface, and Android Studio.
### Node
We recommend installing Node.js and Python2 via [Chocolatey](https://chocolatey.org), a popular package manager for Windows. Open a Command Prompt as Administrator, then run:
```
choco install nodejs.install
choco install python2
```
> You can find additional installation options on [Node.js's Downloads page](https://nodejs.org/en/download/).
<block class="windows linux android" />
### The React Native CLI
Node comes with npm, which lets you install the React Native command line interface.
```
npm install -g react-native-cli
```
### 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](https://developer.android.com/studio/install.html).
#### 2. Confirm the Android SDK is installed
Android Studio installs `Android 7.0 (Nougat)` by default. You can confirm that the SDK was installed by clicking on "Configure" in the last screen in the Android Studio Setup Wizard, or by opening "Preferences" from the Android Studio menu, then choosing `Appearance and Behavior``System Settings``Android SDK`.
![Android Studio SDK Manager](img/react-native-android-studio-configure-sdk-windows.png)
Select "SDK Platforms" from within the SDK Manager and you should see a blue checkmark next to "Android 7.0 (Nougat)". In case it is not, click on the checkbox and then "Apply".
![Android Studio SDK Manager](img/react-native-sdk-platforms.png)
> If you wish to support older versions of Android, you can install additional Android SDKs from this screen.
#### 3. Set up paths
The React Native command line interface requires the `ANDROID_HOME` environment variable to be set up.
<block class="linux android" />
Create or edit your `~/.bashrc` file and add the following lines:
```
export ANDROID_HOME=~/Android/Sdk
export PATH=${PATH}:${ANDROID_HOME}/tools
```
The second line will add the `android` tool to your path, which will come in handy in the next step.
> Please make sure you export the correct path for `ANDROID_HOME` if you did not install the Android SDK using Android Studio.
<block class="windows android" />
Go to `Control Panel``System and Security``System``Change settings`
`Advanced System Settings``Environment variables``New`, then enter the path to your Android SDK.
![env variable](img/react-native-android-sdk-environment-variable-windows.png)
> Please make sure you use the correct path for `ANDROID_HOME` if you did not install the Android SDK using Android Studio.
Restart the Command Prompt to apply the new environment variable.
<block class="linux windows android" />
#### 4. Set up your Android Virtual Device
Android Studio should have set up an Android Virtual Device for you during installation, but it is very common to run into an issue where Android Studio fails to install the AVD.
![Android Studio AVD Manager](img/react-native-tools-avd.png)
To see the list of available AVDs, launch the "AVD Manager" from within Android Studio or run the following command in a terminal:
```
android avd
```
You may follow the [Android Studio User Guide](https://developer.android.com/studio/run/managing-avds.html) to create a new AVD if needed.
> If you see "No system images installed for this target." under CPU/ABI, go back to your "SDK Manager" and click on "Show Package Details" under "SDK Platforms". You will then be able to install any missing system images, such as "Google APIs Intel Atom (x86)".
<block class="linux android" />
### Watchman (optional)
Follow the [Watchman installation guide](https://facebook.github.io/watchman/docs/install.html#build-install) to compile and install Watchman from source.
> [Watchman](https://facebook.github.io/watchman/docs/install.html) is a tool by Facebook for watching
changes in the filesystem. It is highly recommended you install it for better performance, but it's alright to skip this if you find the process to be tedious.
<block class="mac ios android" />
## Testing your React Native Installation
<block class="mac ios" />
Use the React Native command line interface to generate a new React Native project called "AwesomeProject", then run `react-native run-ios` inside the newly created folder.
```
react-native init AwesomeProject
cd AwesomeProject
react-native run-ios
```
You should see your new app running in the iOS Simulator shortly.
`react-native run-ios` is just one way to run your app. You can also run it directly from within Xcode or Nuclide.
<block class="mac android" />
Use the React Native command line interface to generate a new React Native project called "AwesomeProject", then run `react-native run-android` inside the newly created folder.
```
react-native init AwesomeProject
cd AwesomeProject
react-native run-android
```
If everything is set up correctly, you should see your new app running in your Android emulator shortly. `react-native run-android` is just one way to run your app - you can also run it directly from within Android Studio or Nuclide.
<block class="mac ios android" />
### Modifying your app
Now that you have successfully run the app, let's modify it.
<block class="mac ios" />
- Open `index.ios.js` in your text editor of choice and edit some lines.
- Hit `Command⌘ + R` in your iOS Simulator to reload the app and see your change!
<block class="mac android" />
- Open `index.android.js` in your text editor of choice and edit some lines.
- Press the `R` key twice or select `Reload` from the Developer Menu to see your change!
<block class="mac ios android" />
### That's it!
Congratulations! You've successfully run and modified your first React Native app.
<center><img src="img/react-native-congratulations.png" width="150"></img></center>
<block class="windows linux android" />
## Testing your React Native Installation
Use the React Native command line interface to generate a new React Native project called "AwesomeProject", then run `react-native run-android` inside the newly created folder.
```
react-native init AwesomeProject
cd AwesomeProject
react-native run-android
```
If everything is set up correctly, you should see your new app running in your Android emulator shortly.
> A common issue is that the packager is not started automatically when you run
`react-native run-android`. You can start it manually using `react-native start`.
<block class="windows android" />
> If you hit a `ERROR Watcher took too long to load` on Windows, try increasing the timeout in [this file](https://github.com/facebook/react-native/blob/5fa33f3d07f8595a188f6fe04d6168a6ede1e721/packager/react-packager/src/DependencyResolver/FileWatcher/index.js#L16) (under your `node_modules/react-native/`).
<block class="windows linux android" />
### Modifying your app
Now that you have successfully run the app, let's modify it.
- Open `index.android.js` in your text editor of choice and edit some lines.
- Press the `R` key twice or select `Reload` from the Developer Menu to see your change!
### That's it!
Congratulations! You've successfully run and modified a React Native app.
<center><img src="img/react-native-congratulations.png" width="150"></img></center>
<block class="mac windows linux ios android" />
## Now What?
- If you want to add this new React Native code to an existing application, check out the [Integration guide](docs/integration-with-existing-apps.html).
- If you can't get this to work, see the [Troubleshooting](docs/troubleshooting.html#content) page.
- If you're curious to learn more about React Native, continue on
to the [Tutorial](docs/tutorial.html).
<script>
// Convert <div>...<span><block /></span>...</div>
// Into <div>...<block />...</div>
var blocks = document.getElementsByTagName('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 <div>...<block />content<block />...</div>
// Into <div>...<block>content</block><block />...</div>
blocks = document.getElementsByTagName('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 display(type, value) {
var container = document.getElementsByTagName('block')[0].parentNode;
container.className = 'display-' + type + '-' + value + ' ' +
container.className.replace(RegExp('display-' + type + '-[a-z]+ ?'), '');
}
// 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') {
var devOS = null;
var targetPlatform = null;
// 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) {
devOS = 'mac';
} else if (parent.className.indexOf('linux') > -1) {
devOS = 'linux';
} else if (parent.className.indexOf('windows') > -1) {
devOS = 'windows';
} else {
break; // assume we don't have anything.
}
// Target Platform
if (parent.className.indexOf('ios') > -1) {
targetPlatform = 'ios';
} else if (parent.className.indexOf('android') > -1) {
targetPlatform = 'android';
} else {
break; // assume we don't have anything.
}
// We would have broken out if both targetPlatform and devOS hadn't been filled.
display('os', devOS);
display('platform', targetPlatform);
foundHash = true;
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';
display('os', isMac ? 'mac' : (isWindows ? 'windows' : 'linux'));
display('platform', isMac ? 'ios' : 'android');
}
</script>