原理与机制
Application Exploration( 应用探究)
Architecture( 应用架构)
当使用react-native init HelloWorld
创建一个新的应用目录时,它会创建一个新的
HelloWorld.xcodeproj/
Podfile
iOS/
Android/
index.ios.js
index.android.js
node_modules/
package.json
而对于
.....................
RCTRootView *rootView = [[RCTRootView alloc]
initWithBundleURL:jsCodeLocation
moduleName:@"HelloWorld"
launchOptions:launchOptions];
.....................
到这里我们可以看出,
这种架构也就很好地解释了为什么可以动态加载我们的应用,当我们仅仅改变了Command+R
组合键然后重新执行
Virtual Dom 的扩展

虽然<View/>
这个组件会被转化为UIView
组件。
载入JavaScript 代码
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
另一种发布环境下,可以将npm build
:
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
如果在npm start
命令来启动一个动态编译的服务器,如果没有自动启动可以手动的使用npm start
命令,就如定义在
React Native 中的现代JavaScript 代码
从上文中可以看出,
譬如我们以
render: function() {
return (
<View style={styles.container}>
<TextInput
style={styles.nameInput}
onChange={this.onNameChanged}
placeholder='Who should be greeted?'/>
<Text style={styles.welcome}>
Hello, {this.state.name}!</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+Control+Z for dev menu
</Text>
</View>
);
}
在
render: function() {
return (
React.createElement(View, {style: styles.container},
React.createElement(TextInput, {
style: styles.nameInput,
onChange: this.onNameChanged,
placeholder: "Who should be greeted?"}),
React.createElement(Text, {style: styles.welcome},
"Hello, ", this.state.name, "!"),
React.createElement(Text, {style: styles.instructions},
"To get started, edit index.ios.js"
),
React.createElement(Text, {style: styles.instructions},
"Press Cmd+R to reload,", '\n',
"Cmd+Control+Z for dev menu"
)
)
);
}
另一些比较常用的语法转换,一个是模块导入时候的结构器,即我们常常见到模块导入:
var React = require("react-native");
var { AppRegistry, StyleSheet, Text, TextInput, View } = React;
上文中的用法即是所谓的解构赋值,一个简单的例子如下:
var fruits = { banana: "A banana", orange: "An orange", apple: "An apple" };
var { banana, orange, apple } = fruits;
那么我们在某个组件中进行导出的时候,就可以用如下语法:
module.exports.displayName = "Name";
module.exports.Component = Component;
而导入时,即是:
var { Component } = require("component.js");
另一个常用的
AppRegistry.registerComponent("HelloWorld", () => HelloWorld);
会被转化为:
AppRegistry.registerComponent("HelloWorld", function() {
return HelloWorld;
});