You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/EmbeddedApp.md
+43-23Lines changed: 43 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,13 @@ permalink: docs/embeded-app.html
7
7
next: activityindicatorios
8
8
---
9
9
10
+
Since React makes no assumptions about the rest of your technology stack – it’s commonly noted as simply the `V` in `MVC` – it’s easily embeddable within an existing non-React Native app. In fact, it integrates with other best practice community tools like [CocoaPods](http://cocoapods.org/).
[CocoaPods](http://cocoapods.org/) is a package management tool for iOS/Mac development. We need to use it to download React Native. If you haven't install CocoaPods yet, checkout [this tutorial](http://guides.cocoapods.org/using/getting-started.html).
@@ -21,29 +28,32 @@ pod 'React/RCTText'
21
28
22
29
Remember to install all subspecs you need. The `<Text>` element cannot be used without `pod 'React/RCTText'`.
23
30
24
-
Then install pods via shell
31
+
Then install your pods:
25
32
26
33
```
27
-
$ pod install --verbose
34
+
$ pod install
28
35
```
29
36
30
-
The installation process also requires [Node.js](http://nodejs.org).
31
-
32
37
## Create Your React Native App
33
38
34
-
First, enter React Native's pod root directory and create **index.ios.js** inside a directory `ReactComponent`.
39
+
There are two pieces you’ll need to set up:
40
+
41
+
1. The root JavaScript file that will contain your actual React Native app and other components
42
+
- Wrapper Objective-C code that will load up your script and create a `RCTRootView` to display and manage your React Native components
43
+
44
+
First, create a directory for your app’s React code and create a simple `index.ios.js` file:
35
45
36
46
```
37
-
$ cd Pods/React
38
47
$ mkdir ReactComponent
39
48
$ touch index.ios.js
40
49
```
41
50
42
-
Copy & paste following starter code for **index.ios.js**.
51
+
Copy & paste following starter code for `index.ios.js` – it’s a barebones React Native app:
43
52
44
53
```
45
-
var React = require('react-native');
54
+
'use strict';
46
55
56
+
var React = require('react-native');
47
57
var {
48
58
Text,
49
59
View
@@ -58,9 +68,11 @@ var styles = React.StyleSheet.create({
However, let's subclass **UIView** for the sake of clean code. Let's name it **ReactView**. Open up **Yourproject.xcworkspace** and create a new class **ReactView** (You can name it whatever you like :)).
90
+
However, let's subclass `UIView` for the sake of clean code. Let's name it `ReactView`. Open up `Yourproject.xcworkspace` and create a new class `ReactView` (You can name it whatever you like :)).
79
91
80
92
```
81
93
// ReactView.h
@@ -85,7 +97,7 @@ However, let's subclass **UIView** for the sake of clean code. Let's name it **R
85
97
@end
86
98
```
87
99
88
-
Don't forget to add an outlet for it.
100
+
In a view controller that wants to manage this view, go ahead and add an outlet and wire it up:
89
101
90
102
```
91
103
// ViewController.m
@@ -99,19 +111,25 @@ Here I disabled **AutoLayout** for simplicity. In real production world, you sho
99
111
100
112
## Add RCTRootView To Container View
101
113
102
-
Ready for the most interesting part? Now we shall create the **RCTRootView**, where your React Native app lives in.
114
+
Ready for the most interesting part? Now we shall create the `RCTRootView`, where your React Native app lives in.
103
115
104
-
In **ReactView.m**, we need to first initiate **RCTRootView** with the URI of your **index.ios.bundle**. **index.ios.bundle** will be created by packager and served by React Native server, which will be discussed later on.
116
+
In `ReactView.m`, we need to first initiate `RCTRootView` with the URI of your `index.ios.bundle`. `index.ios.bundle` will be created by packager and served by React Native server, which will be discussed later on.
(JS_DIR=`pwd`/ReactComponent; cd Pods/React; npm run start -- --root $JS_DIR)
127
145
```
128
146
129
-
`--root` indicates the root of your React Native apps. Here we just have one **index.ios.js**. React Native development server will use packager to create a **index.ios.bundle**. Which can be access via `http://localhost:8081/index.ios.bundle`.
147
+
This command will start up a React Native development server within our CocoaPods dependency to build our bundled script. The `--root`option indicates the root of your React Native apps – this will be our `ReactComponents` directory containing the single `index.ios.js` file. This running server will package up the `index.ios.bundle` file accessible via `http://localhost:8081/index.ios.bundle`.
130
148
131
149
## Compile And Run
132
150
133
-
Now compile and run your app. You shall now see your React Native app running inside of the **ReactView**.
151
+
Now compile and run your app. You shall now see your React Native app running inside of the `ReactView`.
Live reload works from the simulator, too! You’ve got a simple React component totally encapsulated behind an Objective-C `UIView` subclass.
156
+
137
157
## Conclusion
138
158
139
-
So under the hood, when **RCTRootView** is initialized, it will try to download, parse and run the bundle file from React Native development server. All you need to do is to implement your own container view, add **RCTRootView** as its subclass. And then serve the bundle using React Native development server. Then, bravo!
159
+
So under the hood, when `RCTRootView` is initialized, it will try to download, parse and run the bundle file from React Native development server. This means all you need to do is to implement your own container view or view controller for the `RCTRootView` – the `RCTRootView` ingests your bundled JS and renders your React components. Bravo!
140
160
141
-
You can checkout full source code of sample application [here](https://github.com/tjwudi/EmbededReactNativeExample).
161
+
You can checkout full source code of a sample application [here](https://github.com/tjwudi/EmbededReactNativeExample).
0 commit comments