RN学习笔记02:利用WebStorm创建RN项目
在RN学习笔记01里,安装了node.js与react-native-cli,而且配置了环境变量。
在命令行环境,利用react-native init <项目名>创建项目,执行react-native run-android命令来运行项目,比较繁琐,不如利用WebStorm集成开发环境创建RN项目来得简单方便。
1、新建RN项目RNDemo01
查看入口文件App.js内容:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Fragment} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
const App = () => {
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<Header />
{global.HermesInternal == null ? null : (
<View style={styles.engine}>
<Text style={styles.footer}>Engine: Hermes</Text>
</View>
)}
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Step One</Text>
<Text style={styles.sectionDescription}>
Edit <Text style={styles.highlight}>App.js</Text> to change this
screen and then come back to see your edits.
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>See Your Changes</Text>
<Text style={styles.sectionDescription}>
<ReloadInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Debug</Text>
<Text style={styles.sectionDescription}>
<DebugInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Learn More</Text>
<Text style={styles.sectionDescription}>
Read the docs to discover what to do next:
</Text>
</View>
<LearnMoreLinks />
</View>
</ScrollView>
</SafeAreaView>
</Fragment>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;
2、启动蓝叠模拟器
3、在终端启动项目
切换到蓝叠模拟器,出现“无法加载脚本”的错误提示:
4、打包生成index.android.bundle文件
(1)在项目的android/app/src/main/里创建assets目录
(2)打包生成index.android.bundle文件
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
5、再次运行项目RNDemo01
切换到蓝叠模拟器,查看项目运行效果:
6、修改项目入口文件App.js
7、再次运行项目RNDemo01
切换到蓝叠模拟器,查看运行结果:
蓝叠模拟器似乎不支持RN热加载,每次修改了App.js,必须利用bundle命令重新打包生成index.android.bundle文件,再启动项目,才能看到程序修改后的运行结果,感觉挺烦的。听说夜神模拟器支持RN热加载,赶紧下载来试一试。
8、下载夜神模拟器
9、安装夜神模拟器
10、启动夜神模拟器
11、在终端连接夜神模拟器
12、在WebStorm里启动项目RNDemo01
13、启用热加载功能
14、修改入口文件App.js
切换到夜神模拟器,立马就可以看到修改后的内容:
15、创建RN项目RNDemo02
16、启动项目RNDemo02
17、启动热加载功能
18、修改入口文件App.js
切换到夜神模拟器,查看效果: