react-native语法学习总结

1. 导入React包

import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';

2. 导出一个类给别的模块用和引用的方法

  • 导出
    export default class MyComponent extends Component{
    ...
    }
  • 引用
    import MyComponent from './MyComponent';

注意: 导入和导出的写法必须配套,不能混用!

3. 定义组件

  • 定义: 我们通过定义一个继承自React.Component的class来定义一个组件类
    class Photo extends React.Component {
    render() {
    return (

    );
    }
    }

  • 给组件定义方法
    class Photo extends React.Component {
    componentWillMount() {

      }
      render() {
          return (
                  
          );
      }
    }
    
  • 定义组件的属性类型和默认属性
    class Video extends React.Component {
    static defaultProps = {
    autoPlay: false,
    maxLoops: 10,
    }; // 注意这里有分号
    static propTypes = {
    autoPlay: React.PropTypes.bool.isRequired,
    maxLoops: React.PropTypes.number.isRequired,
    posterFrameSrc: React.PropTypes.string.isRequired,
    videoSrc: React.PropTypes.string.isRequired,
    }; // 注意这里有分号
    render() {
    return (

    );
    } // 注意这里既没有分号也没有逗号
    }

4. 初始化STATE

  class Video extends React.Component {
      state = {
    loopsRemaining: this.props.maxLoops,
    }
  }

不过我们推荐更易理解的在构造函数中初始化(这样你还可以根据需要做一些计算):

  class Video extends React.Component {
    constructor(props){
    super(props);
    this.state = {
        loopsRemaining: this.props.maxLoops,
    };
    }
  }

5. 把方法作为回调提供

通过bind来绑定this引用,或者使用箭头函数(它会绑定当前scope的this引用)来调用

class PostInfo extends React.Component {
    handleOptionsButtonClick(e) {
        this.setState({showOptionsModal:true});
    }

    render() {
        return (
                < TouchableHighlight
        onPress = {this.handleOptionsButtonClick.bind(this)}
        onPress = {e = > this.handleOptionsButtonClick(e)}
        >
         {this.props.label} 
        
        )
    }
}

箭头函数实际上是在这里定义了一个临时的函数,箭头函数的箭头=>之前是一个空括号、单个的参数名、或用括号括起的多个参数名,而箭头之后可以是一个表达式(作为函数的返回值),或者是用花括号括起的函数体(需要自行通过return来返回值,否则返回的是undefined)。

  • 箭头函数的例子

()=>1
v=>v+1
(a,b)=>a+b
()=>{
alert("foo");
}
e=>{
if (e == 0){ return 0; }
return 1000/e;
}

需要注意的是,不论是bind还是箭头函数,每次被执行都返回的是一个新的函数引用,因此如果你还需要函数的引用去做一些别的事情(譬如卸载监听器),那么你必须自己保存这个引用

class PauseMenu extends React.Component{
    constructor(props){
        super(props);
        this._onAppPaused = this.onAppPaused.bind(this);
    }
    componentWillMount(){
        AppStateIOS.addEventListener('change', this._onAppPaused);
    }
    componentDidUnmount(){
        AppStateIOS.removeEventListener('change', this._onAppPaused);
    }
    onAppPaused(event){
    }
}

当然还可以这样写

class PauseMenu extends React.Component{
          componentWillMount(){
                AppStateIOS.addEventListener('change', this.onAppPaused);
    }
    componentDidUnmount(){
        AppStateIOS.removeEventListener('change', this.onAppPaused);
    }
    onAppPaused = (event) => {
        //把方法直接作为一个arrow function的属性来定义,初始化的时候就绑定好了this指针
    }
}

6. Mixins 使用mixin来为我们的类添加一些新的方法

import { Enhance } from "./Enhance";

class MyComponent {
render() {
    if (!this.data) return 
Waiting...
; return
{this.data}
; } } export default Enhance(MyComponent); // Enhanced component

用一个“增强函数”,来某个类增加一些方法,并且返回一个新类,这无疑能实现mixin所实现的大部分需求

7. 解构&属性延展

  • 我们给孩子传递一批属性更为方便了。这个例子把className以外的所有属性传递给div标签:
class AutoloadingPostsGrid extends React.Component {
render() {
    var {
        className,
        ...others,  // contains all properties of this.props except for className
    } = this.props;
    return (
        
); } }

下面这种写法,则是传递所有属性的同时,用覆盖新的className值:

  

这个例子则相反,如果属性中没有包含className,则提供默认的值,而如果属性中已经包含了,则使用属性中的值

你可能感兴趣的:(react-native语法学习总结)