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
切換到夜神模擬器,查看效果: