Now that you have successfully run the app, let's modify it.
index.ios.js in your text editor of choice and edit some lines.⌘R in your iOS Simulator to reload the app and see your changes!index.js in your text editor of choice and edit some lines.⌘R in your iOS Simulator to reload the app and see your changes!index.android.js in your text editor of choice and edit some lines.R key twice or select Reload from the Developer Menu (⌘M) to see your changes!index.js in your text editor of choice and edit some lines.R key twice or select Reload from the Developer Menu (⌘M) to see your changes!Now that you have successfully run the app, let's modify it.
index.android.js in your text editor of choice and edit some lines.R key twice or select Reload from the Developer Menu (⌘M) to see your changes!Now that you have successfully run the app, let's modify it.
index.js in your text editor of choice and edit some lines.R key twice or select Reload from the Developer Menu (⌘M) to see your changes!Congratulations! You've successfully run and modified your first React Native app.

If you get a warning such as "The
swift-2048 [Debug]target overrides theFRAMEWORK_SEARCH_PATHSbuild setting defined inPods/Target Support Files/Pods-swift-2048/Pods-swift-2048.debug.xcconfig. This can lead to problems with the CocoaPods installation", then make sure theFramework Search PathsinBuild Settingsfor bothDebugandReleaseonly contain$(inherited).
Now we will actually modify the native iOS application to integrate React Native. For our 2048 sample app, we will add a "High Score" screen in React Native.
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
index.ios.js file #First, create an empty index.ios.js file in the root of your React Native project.
index.ios.js is the starting point for React Native applications on iOS, and it is always required. It can be a small file that requires other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in index.ios.js.
In your index.ios.js, create your component. In our sample here, we will add simple <Text> component within a styled <View>
Now we will actually modify the native iOS application to integrate React Native. For our 2048 sample app, we will add a "High Score" screen in React Native.
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
index.js file #First, create an empty index.js file in the root of your React Native project.
index.js is the starting point for React Native applications, and it is always required. It can be a small file that requires other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in index.js.
In your index.js, create your component. In our sample here, we will add simple <Text> component within a styled <View>
RNHighScoresis the name of your module that will be used when you add a view to React Native from within your iOS application.
RCTRootView #Now that your React Native component is created via index.ios.js, you need to add that component to a new or existing ViewController. The easiest path to take is to optionally create an event path to your component and then add that component to an existing ViewController.
We will tie our React Native component with a new native view in the ViewController that will actually host it called RCTRootView .
You can add a new link on the main game menu to go to the "High Score" React Native page.

We will now add an event handler from the menu link. A method will be added to the main ViewController of your application. This is where RCTRootView comes into play.
When you build a React Native application, you use the React Native packager to create an index.ios.bundle that will be served by the React Native server. Inside index.ios.bundle will be our RNHighScore module. So, we need to point our RCTRootView to the location of the index.ios.bundle resource (via NSURL) and tie it to the module.
We will, for debugging purposes, log that the event handler was invoked. Then, we will create a string with the location of our React Native code that exists inside the index.ios.bundle. Finally, we will create the main RCTRootView. Notice how we provide RNHighScores as the moduleName that we created above when writing the code for our React Native component.
RNHighScoresis the name of your module that will be used when you add a view to React Native from within your iOS application.
RCTRootView #Now that your React Native component is created via index.js, you need to add that component to a new or existing ViewController. The easiest path to take is to optionally create an event path to your component and then add that component to an existing ViewController.
We will tie our React Native component with a new native view in the ViewController that will actually host it called RCTRootView .
You can add a new link on the main game menu to go to the "High Score" React Native page.

We will now add an event handler from the menu link. A method will be added to the main ViewController of your application. This is where RCTRootView comes into play.
When you build a React Native application, you use the React Native packager to create an index.bundle that will be served by the React Native server. Inside index.bundle will be our RNHighScore module. So, we need to point our RCTRootView to the location of the index.bundle resource (via NSURL) and tie it to the module.
We will, for debugging purposes, log that the event handler was invoked. Then, we will create a string with the location of our React Native code that exists inside the index.bundle. Finally, we will create the main RCTRootView. Notice how we provide RNHighScores as the moduleName that we created above when writing the code for our React Native component.
First import the RCTRootView header.
The
initialPropertiesare here for illustration purposes so we have some data for our high score screen. In our React Native component, we will usethis.propsto get access to that data.
First import the React library.
The
initialPropertiesare here for illustration purposes so we have some data for our high score screen. In our React Native component, we will usethis.propsto get access to that data.
When moving your app to production, the
NSURLcan point to a pre-bundled file on disk via something likelet mainBundle = NSBundle(URLForResource: "main" withExtension:"jsbundle"). You can use thereact-native-xcode.shscript innode_modules/react-native/scripts/to generate that pre-bundled file.
Wire up the new link in the main menu to the newly added event handler method.

One of the easier ways to do this is to open the view in the storyboard and right click on the new link. Select something such as the
Touch Up Insideevent, drag that to the storyboard and then select the created method from the list provided.
You have now done all the basic steps to integrate React Native with your current application. Now we will start the React Native packager to build the index.ios.bundle package and the server running on localhost to serve it.
Apple has blocked implicit cleartext HTTP resource loading. So we need to add the following our project's Info.plist (or equivalent) file.
Wire up the new link in the main menu to the newly added event handler method.

One of the easier ways to do this is to open the view in the storyboard and right click on the new link. Select something such as the
Touch Up Insideevent, drag that to the storyboard and then select the created method from the list provided.
You have now done all the basic steps to integrate React Native with your current application. Now we will start the React Native packager to build the index.bundle package and the server running on localhost to serve it.
Apple has blocked implicit cleartext HTTP resource loading. So we need to add the following our project's Info.plist (or equivalent) file.
Make sure that the path is correct! You shouldn’t run into any “Failed to resolve: com.facebook.react:react-native:0.x.x" errors after running Gradle sync in Android Studio.
Next, make sure you have the Internet permission in your AndroidManifest.xml:
If you need to access to the DevSettingsActivity add to your AndroidManifest.xml:
This is only really used in dev mode when reloading JavaScript from the development server, so you can strip this in release builds if you need to.
Now we will actually modify the native Android application to integrate React Native.
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
index.android.js file #First, create an empty index.android.js file in the root of your React Native project.
index.android.js is the starting point for React Native applications on Android, and it is always required. It can be a small file that requires other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in index.android.js.
In your index.android.js, create your component. In our sample here, we will add simple <Text> component within a styled <View>:
Make sure that the path is correct! You shouldn’t run into any “Failed to resolve: com.facebook.react:react-native:0.x.x" errors after running Gradle sync in Android Studio.
Next, make sure you have the Internet permission in your AndroidManifest.xml:
If you need to access to the DevSettingsActivity add to your AndroidManifest.xml:
This is only really used in dev mode when reloading JavaScript from the development server, so you can strip this in release builds if you need to.
Now we will actually modify the native Android application to integrate React Native.
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
index.js file #First, create an empty index.js file in the root of your React Native project.
index.js is the starting point for React Native applications, and it is always required. It can be a small file that requires other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in index.js.
In your index.js, create your component. In our sample here, we will add simple <Text> component within a styled <View>:
If you are using a starter kit for React Native, replace the "HelloWorld" string with the one in your index.android.js file (it’s the first argument to the
AppRegistry.registerComponent()method).
If you are using Android Studio, use Alt + Enter to add all missing imports in your MyReactActivity class. Be careful to use your package’s BuildConfig and not the one from the ...facebook... package.
We need set the theme of MyReactActivity to Theme.AppCompat.Light.NoActionBar because some components rely on this theme.
If you are using a starter kit for React Native, replace the "HelloWorld" string with the one in your index.js file (it’s the first argument to the
AppRegistry.registerComponent()method).
If you are using Android Studio, use Alt + Enter to add all missing imports in your MyReactActivity class. Be careful to use your package’s BuildConfig and not the one from the ...facebook... package.
We need set the theme of MyReactActivity to Theme.AppCompat.Light.NoActionBar because some components rely on this theme.
Now your activity is ready to run some JavaScript code.
You have now done all the basic steps to integrate React Native with your current application. Now we will start the React Native packager to build the index.android.bundle package and the server running on localhost to serve it.
To run your app, you need to first start the development server. To do this, simply run the following command in the root directory of your React Native project:
Now build and run your Android app as normal.
Once you reach your React-powered activity inside the app, it should load the JavaScript code from the development server and display:

You can use Android Studio to create your release builds too! It’s as easy as creating release builds of your previously-existing native Android app. There’s just one additional step, which you’ll have to do before every release build. You need to execute the following to create a React Native bundle, which will be included with your native Android app:
Don’t forget to replace the paths with correct ones and create the assets folder if it doesn’t exist.
Now just create a release build of your native app from within Android Studio as usual and you should be good to go!
Now your activity is ready to run some JavaScript code.
You have now done all the basic steps to integrate React Native with your current application. Now we will start the React Native packager to build the index.bundle package and the server running on localhost to serve it.
To run your app, you need to first start the development server. To do this, simply run the following command in the root directory of your React Native project:
Now build and run your Android app as normal.
Once you reach your React-powered activity inside the app, it should load the JavaScript code from the development server and display:

You can use Android Studio to create your release builds too! It’s as easy as creating release builds of your previously-existing native Android app. There’s just one additional step, which you’ll have to do before every release build. You need to execute the following to create a React Native bundle, which will be included with your native Android app:
Don’t forget to replace the paths with correct ones and create the assets folder if it doesn’t exist.
Now just create a release build of your native app from within Android Studio as usual and you should be good to go!
At this point you can continue developing your app as usual. Refer to our debugging and deployment docs to learn more about working with React Native.
React Native is like React, but it uses native components instead of web components as building blocks. So to understand the basic structure of a React Native app, you need to understand some of the basic React concepts, like JSX, components, state, and props. If you already know React, you still need to learn some React-Native-specific stuff, like the native components. This
tutorial is aimed at all audiences, whether you have React experience or not.
Let's do this thing.
In accordance with the ancient traditions of our people, we must first build an app that does nothing except say "Hello world". Here it is:
If you are feeling curious, you can play around with sample code directly in the web simulators. You can also paste it into your App.js, index.ios.js, or index.android.js file to create a real app on your local machine.
Some of the things in here might not look like JavaScript to you. Don't panic. This is the future.
First of all, ES2015 (also known as ES6) is a set of improvements to JavaScript that is now part of the official standard, but not yet supported by all browsers, so often it isn't used yet in web development. React Native ships with ES2015 support, so you can use this stuff without worrying about compatibility. import, from, class, extends, and the () => syntax in the example above are all ES2015 features. If you aren't familiar with ES2015, you can probably pick it up just by reading through sample code like this tutorial has. If you want, this page has a good overview of ES2015 features.
The other unusual thing in this code example is <Text>Hello world!</Text>. This is JSX - a syntax for embedding XML within JavaScript. Many frameworks use a special templating language which lets you embed code inside markup language. In React, this is reversed. JSX lets you write your markup language inside code. It looks like HTML on the web, except instead of web things like <div> or <span>, you use React components. In this case, <Text>
-is a built-in component that just displays some text.
So this code is defining HelloWorldApp, a new Component. When you're building a React Native app, you'll be making new components a lot. Anything you see on the screen is some sort of component. A component can be pretty simple - the only thing that's required is a render function which returns some JSX to render.
Good point. To make components do more interesting things, you need to learn about Props.
Improve this page by sending a pull request!