在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:

 /**
* 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'
};
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={this.onclickOne.bind(this)}>
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>
);
}
41 onclickOne(){
42 alert(this.state.name);
43 }
} 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方法3(推荐):

 /**
* 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'
};
}
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>
);
}
41 onclickOne = () =>{
42 alert(this.state.name);
43 }
} 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);

React Native 的绑定 this的更多相关文章

  1. React Native绑定微信分享/登录/支付(演示+实现步骤+注意事项)

    React Native(以下简称RN)绑定微信分享/微信登录/微信支付的实现演示+源码+注意事项!微信的调用大同小异,本文实现了微信的分享功能,其他功能可以在链接文档里面找到具体的方法. 本文分文三 ...

  2. 《React Native 精解与实战》书籍连载「React Native 网络请求与列表绑定」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  3. React Native 与 夜神模拟器的绑定

    之前一直用真机去调试, 每回更新一次都需要手动摇晃手机后才能reload JS, OMG,太麻烦了. 后来寻思模拟器网上推荐用Geny...什么的模拟器,但是那个模拟器还需要VBox一起用. 有点麻烦 ...

  4. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  5. React Native:使用 JavaScript 构建原生应用

    [转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...

  6. React Native之 ScrollView介绍和使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  7. React Native at first sight

    what is React Native? 跟据官方的描述, React Native是一套使用 React 构建 Native app 的编程框架. 推出不久便引发了广泛关注, 这也得益于 Java ...

  8. React Native知识5-Touchable类组件

    React Native 没有像web那样可以给元素绑定click事件,前面我们已经知道Text组件有onPress事件,为了给其他组件 也绑定点击事件,React Native提供了3个组件来做这件 ...

  9. React Native 的ES5 ES6写法对照表

    模块 引用 在ES5里,如果使用CommonJS标准,引入React包基本通过require进行,代码类似这样: //ES5 var React = require("react" ...

随机推荐

  1. 关于正则表达式中参数/g /m的详细分析和例子详解

    总结1:参数/g的用法 表达式加上参数g之后,表明可以进行全局匹配,注意这里"可以"的含义.我们详细叙述: 1)对于表达式对象的exec方法,不加入g,则只返回第一个匹配,无论执行 ...

  2. AutoCAD安装失败

    问题一: Installing .NET Framework Runtime 4.0: D:\安装包\CAD\cad2012(x64)\Map3D2012(x64)\3rdParty\NET\4\wc ...

  3. ORACLE行转列通用过程

    create or replace procedure row_to_col(tabname in varchar2,                                   group_ ...

  4. 详解.Net消息队列(MSMQ)应用

    [IT168 技术文档]MSMQ是Windows 2000.Windows XP.Windows Server 2003的一个组件,并将继续包含在Windows Vista和以后的Windows服务器 ...

  5. [css3]叉叉旋转效果

    .close_frame{display:inline-block;height:14px;width:14px;background:url("../images/closeiframe. ...

  6. iOS_TCP和UDP的详解

    TCP和UDP面试经常被问到,一些初学者也经常问我这种问题,由于TCP协议和UDP协议是基于三次“对话”,解释起来很费劲,所以在这里详细的描述一下自己对TCP协议和UDP协议的理解,如有不妥之处,望指 ...

  7. SQL基础语句(提升)

    1.复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 2.拷贝表 insert into b(a,b,c) select d ...

  8. 自定义cell(xib)中button点击事件不能响应的情况

    遇到这种问题真的好尴尬,之前从来没有遇到过,以为手到擒来,未曾料到还会遇到问题! 好多年没有找到尴尬的感觉,现在找到了,真的很尴尬 !  *o* 1.首先使用场景: 原本没打算用xib,后来为了快速, ...

  9. 3D图形图像处理软件HOOPS介绍及下载

    HOOPS 3D Application Framework(以下简称HOOPS)是建立在OpenGL.Direct3D等图形编程接口之上的更高级别的应用程序框架.不仅为您提供强大的图形功能,还内嵌了 ...

  10. 高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载

    CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会 ...