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:
/**
* 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的更多相关文章
- React Native绑定微信分享/登录/支付(演示+实现步骤+注意事项)
React Native(以下简称RN)绑定微信分享/微信登录/微信支付的实现演示+源码+注意事项!微信的调用大同小异,本文实现了微信的分享功能,其他功能可以在链接文档里面找到具体的方法. 本文分文三 ...
- 《React Native 精解与实战》书籍连载「React Native 网络请求与列表绑定」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- React Native 与 夜神模拟器的绑定
之前一直用真机去调试, 每回更新一次都需要手动摇晃手机后才能reload JS, OMG,太麻烦了. 后来寻思模拟器网上推荐用Geny...什么的模拟器,但是那个模拟器还需要VBox一起用. 有点麻烦 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- React Native:使用 JavaScript 构建原生应用
[转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...
- React Native之 ScrollView介绍和使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native at first sight
what is React Native? 跟据官方的描述, React Native是一套使用 React 构建 Native app 的编程框架. 推出不久便引发了广泛关注, 这也得益于 Java ...
- React Native知识5-Touchable类组件
React Native 没有像web那样可以给元素绑定click事件,前面我们已经知道Text组件有onPress事件,为了给其他组件 也绑定点击事件,React Native提供了3个组件来做这件 ...
- React Native 的ES5 ES6写法对照表
模块 引用 在ES5里,如果使用CommonJS标准,引入React包基本通过require进行,代码类似这样: //ES5 var React = require("react" ...
随机推荐
- c# 判断当前时间是否在 工作日时间段内
#region //获取当前周几 private string _strWorkingDayAM = "08:30";//工作时间上午08:00 private string _s ...
- iOS -初识UIKit
UIKit框架:UI界面上每一个元素都是一个对象 一张图片是UIimageView对象 一段文字是一个UILabei对象 一个按钮是一个UIButton对象 搭建UI界面的步骤 利用类创建对象 将对象 ...
- 关于BigDecimal 的计算
BigDecimal 构造方式主要包括4种: 支持double.int.long等类型计算,废话少说,直接上代码 import java.math.BigDecimal; public class B ...
- PDF 补丁丁 0.5.0.2691 发布(替换字库新增字符映射功能)
新版本在替换 PDF 字体功能中增加了替换字符的功能. 某些 PDF 文档可能由于编码错误的问题,复制出来的文本是乱码. 对于这种文档,可以使用此功能将错误的编码映射到正确的字符上,从而实现修复文档的 ...
- mongodb-replset安装
节点分配 10.110.18.89 10.110.18.90 10.110.18.94 配置文件 systemLog: destination: file path: "/data/mong ...
- Java,Android 项目导入Eclipse常见错误
一.导入的jar包不存在: 右键工程属性查看: 导入的第三方jar包不存在: 解决方法: 删除该jar包,得到该jar包并存至本地,再导入该jar包 二.中文字符乱码: 解决方法: 右键项目属性: 修 ...
- js中setTimeout()时间参数设置为0的探讨
起因源于一道前端笔试题: var fuc = [1,2,3]; for(var i in fuc){ setTimeout(function(){console.log(fuc[i])},0); co ...
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
转自:http://www.chinaitlab.com/Java/line/942440.html 编写多线程程序是为了实现多任务的并发执行,从而能够更好地与用户交互.一般有三种方法,Thread, ...
- erlang 查看内存消耗的方法?
找出消耗内存最多的进程 : lists:reverse(lists:keysort(2,[{P, erlang:process_info(P, heap_size)} || P <- erlan ...
- Calculator(1.0)
Calculator(1.0) Github链接 解题过程中遇到的困难 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序. 不会使用<queue>,在网上查 ...