第一种 在组件(类)的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. 《CSS权威指南(第三版)》---第七章 基本视觉格式化

    主要知识记录: 1.给一个元素指定内容区宽度,如果设置了内边距,边框和外边距,这些因素都会影响CSS的width属性. 2.在水平格式化的7个属性中,width,margin-left,margin- ...

  2. <J2EE学习笔记>续上次Servlet部分提升内容 以及JSP的内容纲要

    以下全部课件均来自于同济大学刘岩老师的<EnterpriseJavaProgramming> 因为授课语言问题,如果翻译有不正确之处欢迎指正 Section 1. 关于Servlet的部分 ...

  3. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  4. ASP.NET 4.0 页面 ValidateRequest="false" 失效不起作用

    当ASP.NET 2.0升级到 ASP.NET 4.0后,页面的 ValidateRequest="false" 不起作用. 因为 ASP.NET 4.0 请求验证被提前到IHtt ...

  5. 关于跨域获取cookie问题的解决

    需求是有2个域名:www.a.com和www.b.com,b.com需要获取a.com中的cookie,解决方法是这样的: 在a.com编写一个设置cookie的页面:set_cookie.php 代 ...

  6. Java_Path_01_路径问题

    二.参考资料 1.java 路径问题 2.Java路径问题最终解决方案—可定位所有资源的相对路径寻址 3.Java获取文件的路径 4.Thread.currentThread().getContext ...

  7. Promise 入门与使用

    Tags: ECMAScript6 参考资料 promises-book Promise对象 we-have-a-problem-with-promises Promise最初被提出是在 E语言中, ...

  8. Hover show tips

    像上面这种效果,hover1时显示2,且1和2有一定间距的东东,一般有两种实现办法: 1.用JS,原理:over1时让2显示,out1时开个定时器延迟500ms再消失,over2时清除定时器,out2 ...

  9. 【Lintcode】118.Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  10. HP SiteScope安装

    下载地址以及安装方法见 http://www.jianshu.com/p/fce30e333578 数据库连接URL:jdbc:mysql://mysql_ip:mysql_port/database ...