第一种 在组件(类)的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. [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...

  2. Laravel的初始化安装

    Laravel的初始化安装 composer 安装 composer中国镜像laravel文档 curl -sS https://getcomposer.org/installer | php # 修 ...

  3. pip 安装模块报错解决

    系统版本 ubuntu  Kylin 16.04 LTS 报错1:安装pip3 安装 Django 总是提示time out,无法安装. 改用国内源: 豆瓣源: sudo pip3 install - ...

  4. iOS审核策略重磅更新:Guideline 2.1批量拒审

    自从进入了2018年,大量应用在AppStore提交审核后,都直接给大家回复了个新春大礼包 Guideline 2.1 - Information Needed. 而大部分的应用,特别是金融类相关AP ...

  5. 分享知识-快乐自己:什么是MVC

    1.什么是mvc: Model View Controller,是模型-视图-控制器的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个组件里,在改进和个性 ...

  6. <%!%>声明的变量和在<%%>中声明的变量的区别

    通过写一个demo,查看Jsp_Servlet源码可知: <%!%>声明的变量是类似类的成员变量,<%%>中的变量是方法中的变量. 参考博客: http://www.cnblo ...

  7. PS 色调— —颜色梯度

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  8. k8s-flannel容器集群网络部署

    [root@k8s-master src]# wget https://github.com/coreos/flannel/releases/download/v0.9.1/flannel-v0.9. ...

  9. The Review Plan I-禁位排列和容斥原理

    The Review Plan I Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB   64-bit integer ...

  10. web.xml中的<jsp-config>的用法详解

    <jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素. 其中<taglib>元素在JSP 1.2时就已 ...