RN学习笔记02:利用WebStorm创建RN项目

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、下载夜神模拟器

https://www.yeshen.com/

9、安装夜神模拟器

10、启动夜神模拟器

11、在终端连接夜神模拟器

12、在WebStorm里启动项目RNDemo01

13、启用热加载功能

14、修改入口文件App.js

切换到夜神模拟器,立马就可以看到修改后的内容:

15、创建RN项目RNDemo02

16、启动项目RNDemo02

17、启动热加载功能

18、修改入口文件App.js

切换到夜神模拟器,查看效果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

howard2005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值