Skip to content

Commit 6e99e31

Browse files
fsonfacebook-github-bot
authored andcommitted
Move HelloWorld template to a single index.js entry point
Summary: This change (initially discussed in expo/create-react-native-app#26) moves the HelloWorld project template from two nearly identical entry points (`index.android.js` and `index.ios.js`) to a single, minimal `index.js` entry point. The root component is created in `App.js`. This unifies the project structure between `react-native init` and Create React Native App and allows CRNA's eject to use the entry point from the HelloWorld template without any hacks to customize it. Also examples in the docs can be just copy-pasted to `App.js` the same way in both HelloWorld and CRNA apps without having to first learn about `AppRegistry.registerComponent`. * Created a new project from the template using `./scripts/test-manual-e2e.sh` and verified that: * The app builds, starts and runs both on Android and iOS. * Editing and reloading changes works. * The new files (`index.js`, `App.js`, `__tests__/App.js`) get created in the project folder. <img width="559" alt="screen shot 2017-08-01 at 19 10 51" src="https://user-images.githubusercontent.com/497214/28835171-300a12b6-76ed-11e7-81b2-623639c3b8f6.png"> <img width="467" alt="screen shot 2017-08-01 at 19 09 12" src="https://user-images.githubusercontent.com/497214/28835180-33d285e0-76ed-11e7-8d68-2b3bc44bf585.png"> <!-- Thank you for sending the PR! If you changed any code, please provide us with clear instructions on how you verified your changes work. In other words, a test plan is *required*. Bonus points for screenshots and videos! Please read the Contribution Guidelines at https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md to learn more about contributing to React Native. Happy contributing! --> Closes #15312 Differential Revision: D5556276 Pulled By: hramos fbshipit-source-id: 068fdf7e51381c2bc50321522f2be0db47296c5e
1 parent f5f5ed5 commit 6e99e31

File tree

20 files changed

+71
-134
lines changed

20 files changed

+71
-134
lines changed

ContainerShip/scripts/run-ci-e2e-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ function e2e_suite() {
230230
# js tests
231231
if [ $RUN_JS -ne 0 ]; then
232232
# Check the packager produces a bundle (doesn't throw an error)
233-
react-native bundle --max-workers 1 --platform android --dev true --entry-file index.android.js --bundle-output android-bundle.js
233+
react-native bundle --max-workers 1 --platform android --dev true --entry-file index.js --bundle-output android-bundle.js
234234
if [ $? -ne 0 ]; then
235235
echo "Could not build android bundle"
236236
return 1
237237
fi
238238

239-
react-native bundle --max-workers 1 --platform ios --dev true --entry-file index.ios.js --bundle-output ios-bundle.js
239+
react-native bundle --max-workers 1 --platform ios --dev true --entry-file index.js --bundle-output ios-bundle.js
240240
if [ $? -ne 0 ]; then
241241
echo "Could not build iOS bundle"
242242
return 1

docs/GettingStarted.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,12 @@ Now that you have successfully run the app, let's modify it.
583583

584584
<block class="native mac ios" />
585585

586-
- Open `index.ios.js` in your text editor of choice and edit some lines.
586+
- Open `index.js` in your text editor of choice and edit some lines.
587587
- Hit `⌘R` in your iOS Simulator to reload the app and see your changes!
588588

589589
<block class="native mac android" />
590590

591-
- Open `index.android.js` in your text editor of choice and edit some lines.
591+
- Open `index.js` in your text editor of choice and edit some lines.
592592
- Press the `R` key twice or select `Reload` from the Developer Menu (`⌘M`) to see your changes!
593593

594594
<block class="native windows linux android" />
@@ -597,7 +597,7 @@ Now that you have successfully run the app, let's modify it.
597597

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

600-
- Open `index.android.js` in your text editor of choice and edit some lines.
600+
- Open `index.js` in your text editor of choice and edit some lines.
601601
- Press the `R` key twice or select `Reload` from the Developer Menu (`⌘M`) to see your changes!
602602

603603
<block class="native mac ios android" />

docs/IntegrationWithExistingApps.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,15 @@ Now we will actually modify the native iOS application to integrate React Native
292292

293293
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.
294294

295-
##### 1. Create a `index.ios.js` file
295+
##### 1. Create a `index.js` file
296296

297-
First, create an empty `index.ios.js` file in the root of your React Native project.
297+
First, create an empty `index.js` file in the root of your React Native project.
298298

299-
`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 `require`s 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`.
299+
`index.js` is the starting point for React Native applications, and it is always required. It can be a small file that `require`s 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`.
300300

301301
##### 2. Add your React Native code
302302

303-
In your `index.ios.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`
303+
In your `index.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`
304304

305305
```javascript
306306
'use strict';
@@ -358,7 +358,7 @@ AppRegistry.registerComponent('MyReactNativeApp', () => RNHighScores);
358358
359359
#### The Magic: `RCTRootView`
360360

361-
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`.
361+
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`.
362362

363363
We will tie our React Native component with a new native view in the `ViewController` that will actually host it called `RCTRootView` .
364364

@@ -372,9 +372,9 @@ You can add a new link on the main game menu to go to the "High Score" React Nat
372372

373373
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.
374374

375-
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.
375+
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.
376376

377-
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](#the-react-native-component) when writing the code for our React Native component.
377+
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](#the-react-native-component) when writing the code for our React Native component.
378378

379379
<block class="objc" />
380380

@@ -389,7 +389,7 @@ First `import` the `RCTRootView` header.
389389
```objectivec
390390
- (IBAction)highScoreButtonPressed:(id)sender {
391391
NSLog(@"High Score Button Pressed");
392-
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"];
392+
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
393393

394394
RCTRootView *rootView =
395395
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
@@ -429,7 +429,7 @@ import React
429429
```swift
430430
@IBAction func highScoreButtonTapped(sender : UIButton) {
431431
NSLog("Hello")
432-
let jsCodeLocation = URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")
432+
let jsCodeLocation = URL(string: "http://localhost:8081/index.bundle?platform=ios")
433433
let mockData:NSDictionary = ["scores":
434434
[
435435
["name":"Alex", "value":"42"],
@@ -471,7 +471,7 @@ Wire up the new link in the main menu to the newly added event handler method.
471471
472472
### Test your integration
473473

474-
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.
474+
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.
475475

476476
##### 1. Add App Transport Security exception
477477

@@ -586,15 +586,15 @@ Now we will actually modify the native Android application to integrate React Na
586586

587587
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.
588588

589-
##### 1. Create a `index.android.js` file
589+
##### 1. Create a `index.js` file
590590

591-
First, create an empty `index.android.js` file in the root of your React Native project.
591+
First, create an empty `index.js` file in the root of your React Native project.
592592

593-
`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 `require`s 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`.
593+
`index.js` is the starting point for React Native applications, and it is always required. It can be a small file that `require`s 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`.
594594

595595
##### 2. Add your React Native code
596596

597-
In your `index.android.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`:
597+
In your `index.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`:
598598

599599
```javascript
600600
'use strict';
@@ -679,7 +679,7 @@ public class MyReactActivity extends Activity implements DefaultHardwareBackBtnH
679679
mReactInstanceManager = ReactInstanceManager.builder()
680680
.setApplication(getApplication())
681681
.setBundleAssetName("index.android.bundle")
682-
.setJSMainModuleName("index.android")
682+
.setJSMainModuleName("index")
683683
.addPackage(new MainReactPackage())
684684
.setUseDeveloperSupport(BuildConfig.DEBUG)
685685
.setInitialLifecycleState(LifecycleState.RESUMED)
@@ -696,7 +696,7 @@ public class MyReactActivity extends Activity implements DefaultHardwareBackBtnH
696696
}
697697
```
698698

699-
> 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).
699+
> 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).
700700
701701
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.
702702

@@ -775,7 +775,7 @@ Now your activity is ready to run some JavaScript code.
775775

776776
### Test your integration
777777

778-
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.
778+
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.
779779

780780
##### 1. Run the packager
781781

@@ -798,7 +798,7 @@ Once you reach your React-powered activity inside the app, it should load the Ja
798798
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:
799799

800800
```
801-
$ react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/com/your-company-name/app-package-name/src/main/assets/index.android.bundle --assets-dest android/com/your-company-name/app-package-name/src/main/res/
801+
$ react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/com/your-company-name/app-package-name/src/main/assets/index.android.bundle --assets-dest android/com/your-company-name/app-package-name/src/main/res/
802802
```
803803

804804
> Don’t forget to replace the paths with correct ones and create the assets folder if it doesn’t exist.

docs/Tutorial.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In accordance with the ancient traditions of our people, we must first build an
1919

2020
```ReactNativeWebPlayer
2121
import React, { Component } from 'react';
22-
import { AppRegistry, Text } from 'react-native';
22+
import { Text } from 'react-native';
2323
2424
export default class HelloWorldApp extends Component {
2525
render() {
@@ -28,12 +28,9 @@ export default class HelloWorldApp extends Component {
2828
);
2929
}
3030
}
31-
32-
// skip this line if using Create React Native App
33-
AppRegistry.registerComponent('AwesomeProject', () => HelloWorldApp);
3431
```
3532

36-
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.
33+
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` file to create a real app on your local machine.
3734

3835
## What's going on here?
3936

@@ -48,14 +45,6 @@ is a built-in component that just displays some text.
4845

4946
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.
5047

51-
<div class="banner-crna-ejected">
52-
<h3>Projects With Native Code Only</h3>
53-
<p>
54-
In the particular example above, <code>HelloWorldApp</code> is registered with the <code>AppRegistry</code>. The <code>AppRegistry</code> just tells React Native which component is the root one for the whole application. It's included in these examples so you can paste the whole thing into your <code>index.ios.js</code> or <code>index.android.js</code> file and get it running. If you have a project from Create React Native App, this is handled for you and it's not necessary to call <code>AppRegistry</code> in your code.
55-
</p>
56-
</div>
57-
58-
5948
## This app doesn't do very much
6049

6150
Good point. To make components do more interesting things, you need to [learn about Props](docs/props.html).

local-cli/generator/copyProjectTemplateAndReplace.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function copyProjectTemplateAndReplace(srcPath, destPath, newProjectName, option
4444
// This also includes __tests__/index.*.js
4545
if (fileName === 'index.ios.js') { return; }
4646
if (fileName === 'index.android.js') { return; }
47+
if (fileName === 'index.js') { return; }
48+
if (fileName === 'App.js') { return; }
4749
}
4850

4951
const relativeFilePath = path.relative(srcPath, absoluteSrcFilePath);

local-cli/templates/HelloNavigation/views/MainNavigator.js renamed to local-cli/templates/HelloNavigation/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import React, { Component } from 'react';
1010
import { StackNavigator } from 'react-navigation';
1111

12-
import HomeScreenTabNavigator from './HomeScreenTabNavigator';
13-
import ChatScreen from './chat/ChatScreen';
12+
import HomeScreenTabNavigator from './views/HomeScreenTabNavigator';
13+
import ChatScreen from './views/chat/ChatScreen';
1414

1515
/**
1616
* Top-level navigator. Renders the application UI.
1717
*/
18-
const MainNavigator = StackNavigator({
18+
const App = StackNavigator({
1919
Home: {
2020
screen: HomeScreenTabNavigator,
2121
},
@@ -24,4 +24,4 @@ const MainNavigator = StackNavigator({
2424
},
2525
});
2626

27-
export default MainNavigator;
27+
export default App;

local-cli/templates/HelloNavigation/index.android.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

local-cli/templates/HelloNavigation/index.ios.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

local-cli/templates/HelloWorld/index.android.js renamed to local-cli/templates/HelloWorld/App.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@
66

77
import React, { Component } from 'react';
88
import {
9-
AppRegistry,
9+
Platform,
1010
StyleSheet,
1111
Text,
1212
View
1313
} from 'react-native';
1414

15-
export default class HelloWorld extends Component {
15+
const instructions = Platform.select({
16+
ios: 'Press Cmd+R to reload,\n' +
17+
'Cmd+D or shake for dev menu',
18+
android: 'Double tap R on your keyboard to reload,\n' +
19+
'Shake or press menu button for dev menu',
20+
});
21+
22+
export default class App extends Component {
1623
render() {
1724
return (
1825
<View style={styles.container}>
1926
<Text style={styles.welcome}>
2027
Welcome to React Native!
2128
</Text>
2229
<Text style={styles.instructions}>
23-
To get started, edit index.android.js
30+
To get started, edit App.js
2431
</Text>
2532
<Text style={styles.instructions}>
26-
Double tap R on your keyboard to reload,{'\n'}
27-
Shake or press menu button for dev menu
33+
{instructions}
2834
</Text>
2935
</View>
3036
);
@@ -49,5 +55,3 @@ const styles = StyleSheet.create({
4955
marginBottom: 5,
5056
},
5157
});
52-
53-
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import 'react-native';
22
import React from 'react';
3-
import Index from '../index.ios.js';
3+
import App from '../App';
44

55
// Note: test renderer must be required after react-native.
66
import renderer from 'react-test-renderer';
77

88
it('renders correctly', () => {
99
const tree = renderer.create(
10-
<Index />
10+
<App />
1111
);
1212
});

0 commit comments

Comments
 (0)