第一种 在组件(类)的constructor中绑定this

 class Demo extends Component {
constructor(this) {
super(this)
this.state = { value: 0 }
this.handleDivClick = this.handleDivClick.bind(this)
} handleDivClick() {
this.setState((state, props) => ({ value: state.value + 1 }))
} render() {
const { value } = this.state
return <div onClick={this.handleDivClick}>{value}</div>
}
}

第二种 使用 public class fields 语法(实验性质)

 class Demo extends Component {
... handleClick = () => {
console.log('"this" is: ', this)
} render() {
return <button onClick={this.handleClick}>Get log</button>
}
}

关于类字段语法(参考Babel官网):

 class Book {
// Property initializer syntax (属性初始化器语法)
instanceProperty = 'book'
boundFunction = () => {
return this.instanceProperty
} // Static class properties (静态属性)
static staticProperty = 'pencil'
static staticFunction = function() {
return Bork.staticProperty
}
} let myBork = new Bork // Property initializers are not on the prototype.
console.log(myBork.__proto__.boundFunction) // undefined // Bound functions are bound to the class instance.
console.log(myBork.boundFunction.call(undefined)) // 'bork' // Static function exists on the class.
console.log(Bork.staticFunction()) // 'pencil'

第三种 使用箭头函数

 class Demo extends Component {
... handleClick() {
// ...
} render() {
return (
<button onClick={e => this.handleClick(e)}>Click me</button>
)
}
}

绑定事件的传参

 // 1.
<button onClick={e => this.handleClick(e, id)}>Click</button> // 2.
<button onClick={this.handleClick.bind(this, id)}>Click</button>

React中方法的this绑定的更多相关文章

  1. Swift中方法(method)所谓的lazy绑定简介

    我们知道在ruby之类的动态语言中对象方法可以先从类中预先抽取,然后再应用到某个具体对象上.这称为无绑定的method对象,也可以叫做lazy绑定. 下面举个例子: irb(main):004:0&g ...

  2. React组件方法中为什么要绑定this

    如果你尝试使用过React进行前端开发,一定见过下面这样的代码: //假想定义一个ToggleButton开关组件 class ToggleButton extends React.Component ...

  3. 理解React中es6方法创建组件的this

    首发于:https://mingjiezhang.github.io/(转载请说明此出处). 在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的. 从react中的 ...

  4. React中的响应式设计思想和事件绑定

    这两个点是react入门非常重要的两个点,以前我们是直接操作dom的形式去做,react的设计思想和以前直接操作dom是完全不同的,react是一个响应式的框架,他在做编程的时候,强调的是我们不要直接 ...

  5. React中ref的使用方法

    React中ref的使用方法 在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像reac ...

  6. react中直接调用子组件的方法(非props方式)

    我们都知道在 react中,若要在父组件调用子组件的方法,通常我们会采用在父组件定义一个方法,作为props转给子组件,然后执行该方法,可以获取到子组件传回的参数以得到我们的目的. 显而易见,这个执行 ...

  7. React 中的 AJAX 请求:获取数据的方法

    React 中的 AJAX 请求:获取数据的方法 React 只是使用 props 和 state 两处的数据进行组件渲染. 因此,想要使用来自服务端的数据,必须将数据放入组件的 props 或 st ...

  8. 【译】在React中实现条件渲染的7种方法

    原文地址:https://scotch.io/tutorials/7-ways-to-implement-conditional-rendering-in-react-applications 借助R ...

  9. React中引用CSS样式的方法

    相对于html中引用css的三种方法,react中也有三种方法,一一相对: 1. 行内样式:直接在组件内部定义 <div style={{width:'20px',height:'30px'}} ...

随机推荐

  1. CSS3 3D下拉折叠菜单

    在线演示 本地下载

  2. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  3. Discuz X3游客看小图功能导致文字内容隐藏的【修复方法】

    如果帖子内容以图片开始,并且开启了游客看小图功能,那么图片下面的文字也会被隐藏,这是不科学的(在图片上边的文字不会被隐藏)查看DZ源代码,发现是程序猿疏忽漏掉了</div> 下边1314学 ...

  4. 算法(Algorithms)第4版 练习 1.3.25 1.3.24

    代码实现: //1.3.24 /** * remove the node following the node x * (and does nothing if the argument or the ...

  5. BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...

  6. linux应用之Lamp(apache+mysql+php)的源码安装(centos)

    Linux+Apache+Mysql+Php源码安装 一.安装环境: 系统:Centos6.5x64 Apache: httpd-2.4.10.tar.gz Mysql: mysql-5.6.20-l ...

  7. UVA 111 简单DP 但是有坑

    题目传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18201 其实是一道不算难的DP,但是搞了好久,才发现原来是题目没 ...

  8. 练习E-R图书管理数据库

  9. win32com操作word(2):常用用法

    一.对象的位置关系: 1.Range属性位于(部分): Selection__Section__Cell__Paragraph__Table__Bookmark__Comment__Row__List ...

  10. codewar代码练习1——8级晋升7级

    最近发现一个不错的代码练习网站codewar(http://www.codewars.com).注册了一个账号,花了几天的茶余饭后时间做题,把等级从8级升到了7级.本文的目的主要介绍使用感受及相应题目 ...