React Native 的绑定 this

在React Native开发中,如果使用ES6语法的话,最好绑定this.但是使用ES5语法的话不需要绑定this.因为ES5会autobinding.

this所指的就是直至包含this指针的上层对象.

绑定this方法1:

复制代码
/**
 * 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 myProject extends Component {
  // 构造
    constructor(props) {
      super(props);
      // 初始状态
      this.state = {
        name:'shaoting',
          job:'coding'
      };
      //如果使用ES6编码 且 自定义方法里面需要使用到this .这时需要绑定this,否则报错
      //绑定this
        this.onclickOne = this.onclickOne.bind(this);
    }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome} onPress={this.onclickOne}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
    onclickOne(){
       alert(this.state.name);
    }
}

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('myProject', () => myProject);
复制代码

绑定this方法2:

复制代码
 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  * @flow
 5  */
 6 
 7 import React, { Component } from 'react';
 8 import {
 9   AppRegistry,
10   StyleSheet,
11   Text,
12   View
13 } from 'react-native';
14 
15 export default class myProject extends Component {
16   // 构造
17     constructor(props) {
18       super(props);
19       // 初始状态
20       this.state = {
21         name:'shaoting',
22           job:'coding'
23       };
24     }
25   render() {
26     return (
27       <View style={styles.container}>
28         <Text style={styles.welcome} onPress={this.onclickOne.bind(this)}>
29           Welcome to React Native!
30         </Text>
31         <Text style={styles.instructions}>
32           To get started, edit index.ios.js
33         </Text>
34         <Text style={styles.instructions}>
35           Press Cmd+R to reload,{'\n'}
36           Cmd+D or shake for dev menu
37         </Text>
38       </View>
39     );
40   }
41     onclickOne(){
42        alert(this.state.name);
43     }
44 }
45 
46 const styles = StyleSheet.create({
47   container: {
48     flex: 1,
49     justifyContent: 'center',
50     alignItems: 'center',
51     backgroundColor: '#F5FCFF',
52   },
53   welcome: {
54     fontSize: 20,
55     textAlign: 'center',
56     margin: 10,
57   },
58   instructions: {
59     textAlign: 'center',
60     color: '#333333',
61     marginBottom: 5,
62   },
63 });
64 
65 AppRegistry.registerComponent('myProject', () => myProject);
复制代码

 

绑定this方法3(推荐):

复制代码
 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  * @flow
 5  */
 6 
 7 import React, { Component } from 'react';
 8 import {
 9   AppRegistry,
10   StyleSheet,
11   Text,
12   View
13 } from 'react-native';
14 
15 export default class myProject extends Component {
16   // 构造
17     constructor(props) {
18       super(props);
19       // 初始状态
20       this.state = {
21         name:'shaoting',
22           job:'coding'
23       };
24     }
25   render() {
26     return (
27       <View style={styles.container}>
28         <Text style={styles.welcome} onPress={this.onclickOne}>
29           Welcome to React Native!
30         </Text>
31         <Text style={styles.instructions}>
32           To get started, edit index.ios.js
33         </Text>
34         <Text style={styles.instructions}>
35           Press Cmd+R to reload,{'\n'}
36           Cmd+D or shake for dev menu
37         </Text>
38       </View>
39     );
40   }
41     onclickOne = () =>{
42        alert(this.state.name);
43     }
44 }
45 
46 const styles = StyleSheet.create({
47   container: {
48     flex: 1,
49     justifyContent: 'center',
50     alignItems: 'center',
51     backgroundColor: '#F5FCFF',
52   },
53   welcome: {
54     fontSize: 20,
55     textAlign: 'center',
56     margin: 10,
57   },
58   instructions: {
59     textAlign: 'center',
60     color: '#333333',
61     marginBottom: 5,
62   },
63 });
64 
65 AppRegistry.registerComponent('myProject', () => myProject);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值