Skip to content

Commit 99b2a10

Browse files
committed
Merge pull request #552 from andrewsardone/aps-embedded-app
Improve EmbeddedApp.md docs wrt location of ReactComponent (also minor formatting)
2 parents b82de1e + 2536716 commit 99b2a10

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

docs/EmbeddedApp.md

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ permalink: docs/embeded-app.html
77
next: activityindicatorios
88
---
99

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/).
11+
12+
## Requirements
13+
14+
- [CocoaPods](http://cocoapods.org/)`gem install cocoapods`
15+
- [Node.js](http://nodejs.org)`brew install node`
16+
1017
## Install React Native Using CocoaPods
1118

1219
[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'
2128

2229
Remember to install all subspecs you need. The `<Text>` element cannot be used without `pod 'React/RCTText'`.
2330

24-
Then install pods via shell
31+
Then install your pods:
2532

2633
```
27-
$ pod install --verbose
34+
$ pod install
2835
```
2936

30-
The installation process also requires [Node.js](http://nodejs.org).
31-
3237
## Create Your React Native App
3338

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:
3545

3646
```
37-
$ cd Pods/React
3847
$ mkdir ReactComponent
3948
$ touch index.ios.js
4049
```
4150

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:
4352

4453
```
45-
var React = require('react-native');
54+
'use strict';
4655
56+
var React = require('react-native');
4757
var {
4858
Text,
4959
View
@@ -58,9 +68,11 @@ var styles = React.StyleSheet.create({
5868
5969
class SimpleApp extends React.Component {
6070
render() {
61-
return <View style={styles.container}>
71+
return (
72+
<View style={styles.container}>
6273
<Text>This is a simple application.</Text>
63-
</View>;
74+
</View>
75+
)
6476
}
6577
}
6678
@@ -71,11 +83,11 @@ React.AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
7183

7284
## Add Container View To Your App
7385

74-
You should now add container view for React Native component. It can be any **UIView** in your app.
86+
You should now add a container view for the React Native component. It can be any `UIView` in your app.
7587

7688
![Container view example](/react-native/img/EmbeddedAppContainerViewExample.png)
7789

78-
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 :)).
7991

8092
```
8193
// ReactView.h
@@ -85,7 +97,7 @@ However, let's subclass **UIView** for the sake of clean code. Let's name it **R
8597
@end
8698
```
8799

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:
89101

90102
```
91103
// ViewController.m
@@ -99,19 +111,25 @@ Here I disabled **AutoLayout** for simplicity. In real production world, you sho
99111

100112
## Add RCTRootView To Container View
101113

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.
103115

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.
105117

106118
```
107-
NSString *urlString = @"http://localhost:8081/index.ios.bundle";
108-
NSURL *jsCodeLocation = [NSURL URLWithString:urlString];
119+
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
120+
// For production use, this `NSURL` could instead point to a pre-bundled file on disk:
121+
//
122+
// NSURL *jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
123+
//
124+
// To generate that file, run the curl command and add the output to your main Xcode build target:
125+
//
126+
// curl http://localhost:8081/index.ios.bundle -o main.jsbundle
109127
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
110128
moduleName: @"SimpleApp"
111129
launchOptions:nil];
112130
```
113131

114-
Then add it as a subview of the **ReactView**.
132+
Then add it as a subview of the `ReactView`.
115133

116134
```
117135
[self addSubview:rootView];
@@ -123,19 +141,21 @@ rootView.frame = self.bounds;
123141
In root directory, we need to start React Native development server.
124142

125143
```
126-
$ ./Pods/React/packager/packager.sh --root ./ReactComponents
144+
(JS_DIR=`pwd`/ReactComponent; cd Pods/React; npm run start -- --root $JS_DIR)
127145
```
128146

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`.
130148

131149
## Compile And Run
132150

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`.
134152

135153
![Example](/react-native/img/EmbeddedAppExample.png)
136154

155+
Live reload works from the simulator, too! You’ve got a simple React component totally encapsulated behind an Objective-C `UIView` subclass.
156+
137157
## Conclusion
138158

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!
140160

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

Comments
 (0)