React Native,第一个Hello word程序
2017-02-26 15:13
686 查看
在上一篇博客中,介绍了React Native的环境搭建。搭建好后,下面开始第一个程序,Hello word程序(开发环境:windows 7)
1.使用android studio新建一个项目,注意项目要在android目录下面(原因是在此目录下最后执行命令react-native run-android才能跑起来,否则的话报错Android project not found. Maybe run react-native
android first?)
2.项目配置。参考自己搭建环境运行起来的程序里面的文件。
下面是我上篇博客中搭建环境成功时跑的一个项目
本次Hello word程序也仿照这么写。
最后在清单文件中加一个访问网络的权限 <uses-permission android:name="android.permission.INTERNET" />
3.JS需要的配置
在上一片博客中,正常的react native(android)项目还有这样几个文件
下面来一一生成
3.1生成package.json文件
在根目录下面执行npm init (name注意小写,其他的直接回车即可)
将上一片博客中的项目里package.json文件里的一些内容拷到我们的package.json文件里
我这里拷贝了start和dependencies(注意json文件格式,注意逗号),保存
3.2生成node_modules目录
在根目录执行npm install命令(或者直接拷贝node_modules目录也是可以的)
注: http://blog.csdn.net/fangfanggaogao/article/details/51508441 npm install命令报错的话参考这篇博客
运行成功后生成了node_modules目录
4.在根目录下创建index.android.js文件
并添加下面代码
5.flow配置
curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
做一下flow配置
注:curl命令执行出问题,下载curl工具,配置一下环境变量即可
6.最后在根目录执行命令react-native run-android命令就可以了
注意:package.json文件里的name值,index.android.js里的AppRegistry.registerComponent()方法的第一个参数,MainActivity里getMainComponentName()方法的返回值要一样,我这里写的都是helloword
1.使用android studio新建一个项目,注意项目要在android目录下面(原因是在此目录下最后执行命令react-native run-android才能跑起来,否则的话报错Android project not found. Maybe run react-native
android first?)
2.项目配置。参考自己搭建环境运行起来的程序里面的文件。
下面是我上篇博客中搭建环境成功时跑的一个项目
本次Hello word程序也仿照这么写。
最后在清单文件中加一个访问网络的权限 <uses-permission android:name="android.permission.INTERNET" />
3.JS需要的配置
在上一片博客中,正常的react native(android)项目还有这样几个文件
下面来一一生成
3.1生成package.json文件
在根目录下面执行npm init (name注意小写,其他的直接回车即可)
将上一片博客中的项目里package.json文件里的一些内容拷到我们的package.json文件里
我这里拷贝了start和dependencies(注意json文件格式,注意逗号),保存
3.2生成node_modules目录
在根目录执行npm install命令(或者直接拷贝node_modules目录也是可以的)
注: http://blog.csdn.net/fangfanggaogao/article/details/51508441 npm install命令报错的话参考这篇博客
运行成功后生成了node_modules目录
4.在根目录下创建index.android.js文件
并添加下面代码
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from 'react-native'; export default class myrn extends Component { render() { return ( <View style={styles.container}> <Text style={styles.hello}>Hello, World</Text> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, }); AppRegistry.registerComponent('helloword', () => myrn);
5.flow配置
curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
做一下flow配置
注:curl命令执行出问题,下载curl工具,配置一下环境变量即可
6.最后在根目录执行命令react-native run-android命令就可以了
注意:package.json文件里的name值,index.android.js里的AppRegistry.registerComponent()方法的第一个参数,MainActivity里getMainComponentName()方法的返回值要一样,我这里写的都是helloword
相关文章推荐
- 第一个React Native程序踩到的那些坑
- 运行第一个React Native程序
- 从零学React Native之01创建第一个程序
- react-native(2)---创建第一个程序helloWorld
- React Native运行第一个程序
- 最完整的Windows下React Native开发环境搭建与第一个程序HelloWorld
- 命令行创建第一个react-navite程序
- ReactNative 从环境和第一个demo说起,填坑教程
- React ——第一个程序
- 第一个React程序HelloWorld
- React-Native开发之运行你的第一个项目
- react Native 运行程序报错解决笔记
- React-Native的环境配置与第一个HelloWorld
- struts2 第一个hello word 程序
- [React Native混合开发]React Native for iOS之创建第一个案例
- React native 第一个坑记录
- 在终端terminal运行ReactNative程序经常出现的问题
- [ReactNative入门到精通]React Native 第一个程序 Hello Word
- windows phone7第一个程序hello word
- 【React Native开发】React Native For Android环境配置以及第一个实例