我的React-Native不得不说的一些事情-1

2016-03-20  本文已影响2189人  三月懒驴

创建文档时间:2016.3.19-23:18
作者:三月懒驴
使用平台:Mac

1. 安装

//前提:安装node 再安装react-native命令行工具
npm init  //随便填写回车下去。
npm install -g react-native-cli
//不用翻墙的安装方法
mkdir react_native
cd react_native
npm install react-native --save
node -e "require('react-native/local-cli/cli').init('.','MyProject')"
//翻墙的可以这样:
react-native init
//运行:Mac
在项目文件里面的IOS文件夹用Xcode打开MyProject.xcodeproj,在Xcode里面运行  

2. 组件

组件这个概念是非常棒的!下面我们来看看react-native自带的一些组件。 这里面只说部分我自己用过的,基本都是IOS平台的一些自带组件

3. API

提供了一个通用的接口来与传入和传出的App链接进行交互。如果想要在网页中告诉系统要打开你的APP,可以使用这个

4. 课间总结

上面一共介绍了20多种组件和API。而实际上,我们初学者可以把他们分为几类。基础,重点和额外。基础是必须掌握的,重点是在应用中普遍用到的,而额外的话是根据实际使用情况使用。

5.编程

5.1 进入Hello World!编辑:index.ios.js


'use strict'
//下面要做三件事:引入模块,定义一个组件,把组件注册到APP去。
//但是为了让视觉好看点,我们要用styleSheet来进行布局,
//因此在定义组件之后,我们可以定义一个样式对象,再进行注册到APP去

//1.引入模块(PS:0.2之后全面使用ES6语法了)
import React from 'react-native'
let {AppRegistry,Component,StyleSheet,Text,View} = React;

//2.定义组件
class MyProject extends Component{
    render(){
        return(
            <View style={styles.body}>
                <Text style={styles.text}>Hello World!</Text>
            </View>
        )
    }
}

//3. 定义样式对象
const styles = {
    body:{
        flex:1,//让内部元素都带有相同的长度,忽略它们的内容:
        justifyContent:'center',//内部元素在主轴上的对齐方式。
        alignItems:'center',//内部元素在交叉轴上如何对齐。
        backgroundColor:'#ccc',
    },
    text:{
        fontSize:20,
        textAlign:'center'
    }
}

//4. 注册:这里的第一个参数是你生成项目时候起的名字,
//第二个参数是ES6的箭头函数。()=> XXX 等价 function(){}
AppRegistry.registerComponent('MyProject',()=> MyProject);

5.2 的进阶:把组件分离出去

//touch component.js --》新建一个文件
把上一个例子的组件部分代码分离出来
//in component.js
'use strict'
import React from 'react-native'
let {StyleSheet,Text,View} = React
export default class Test extends Component{
render(){
    return(
        <View style={styles.body}>
            <Text style= {styles.text}>Hello World!</Text>
        </View>
    )
}
}
let styles = {
 body:{
     flex:1,
     justifyContent:'center',
     alignItems:'center',
     backgroundColor:'#fcfcfc',
 },
 text:{
     fontSize:40,
     textAlign:'center'
 }
}
//in index.ios.js

'usr strict'
//1.引入模块和组件
import React from 'react-native'
import TestComponent from './component'
let {AppRegistry,Component,StyleSheet,Text,View} = React;

//2.定义组件
class MyProject extends Component{
    render(){
        return(
            <TestComponent />
        )
    }
}

//3. 注册
AppRegistry.registerComponent('MyProject',()=> MyProject);

未完待续...

上一篇 下一篇

猜你喜欢

热点阅读