React中方法的this绑定
第一种 在组件(类)的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绑定的更多相关文章
- Swift中方法(method)所谓的lazy绑定简介
我们知道在ruby之类的动态语言中对象方法可以先从类中预先抽取,然后再应用到某个具体对象上.这称为无绑定的method对象,也可以叫做lazy绑定. 下面举个例子: irb(main):004:0&g ...
- React组件方法中为什么要绑定this
如果你尝试使用过React进行前端开发,一定见过下面这样的代码: //假想定义一个ToggleButton开关组件 class ToggleButton extends React.Component ...
- 理解React中es6方法创建组件的this
首发于:https://mingjiezhang.github.io/(转载请说明此出处). 在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的. 从react中的 ...
- React中的响应式设计思想和事件绑定
这两个点是react入门非常重要的两个点,以前我们是直接操作dom的形式去做,react的设计思想和以前直接操作dom是完全不同的,react是一个响应式的框架,他在做编程的时候,强调的是我们不要直接 ...
- React中ref的使用方法
React中ref的使用方法 在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像reac ...
- react中直接调用子组件的方法(非props方式)
我们都知道在 react中,若要在父组件调用子组件的方法,通常我们会采用在父组件定义一个方法,作为props转给子组件,然后执行该方法,可以获取到子组件传回的参数以得到我们的目的. 显而易见,这个执行 ...
- React 中的 AJAX 请求:获取数据的方法
React 中的 AJAX 请求:获取数据的方法 React 只是使用 props 和 state 两处的数据进行组件渲染. 因此,想要使用来自服务端的数据,必须将数据放入组件的 props 或 st ...
- 【译】在React中实现条件渲染的7种方法
原文地址:https://scotch.io/tutorials/7-ways-to-implement-conditional-rendering-in-react-applications 借助R ...
- React中引用CSS样式的方法
相对于html中引用css的三种方法,react中也有三种方法,一一相对: 1. 行内样式:直接在组件内部定义 <div style={{width:'20px',height:'30px'}} ...
随机推荐
- vue 动态传值笔记
:prop="'answers.a' + item.split('.')[1]+'.total'" {{scope.row.answers['a'+item.split('.')[ ...
- 20145239 《Java程序设计》实验三 实验报告
详见我的parter20145224的博客:http://www.cnblogs.com/20145224kevs/p/5428892.html 感想:这次的实验看似容易,但很多点都需要注意,比如开源 ...
- BZOJ 1726 [Usaco2006 Nov]Roadblocks第二短路:双向spfa【次短路】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1726 题意: 给你一个无向图,求次短路. 题解: 两种方法. 方法一: 一遍spfa,在s ...
- ES搜索排序,文档相关度评分介绍——Vector Space Model
Vector Space Model The vector space model provides a way of comparing a multiterm query against a do ...
- 插件_热部署_JRebel
一.License Server 1.运行反向代理 GitHub地址:https://github.com/ilanyu/ReverseProxy 下载地址:ReverseProxy_windows_ ...
- codeforces 706A A. Beru-taxi(水题)
题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...
- Linux网络编程socket错误分析
socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...
- CodeForces - 613D:Kingdom and its Cities(虚树+DP)
Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...
- 基于Qt Gui的Led控制显示程序
基于arm + linux的嵌入式软件开发,基本上的内容主要是:u-boot的移植,kernel的裁剪和相关驱动程序的设计,root-fs的制作,应用程序的设计,其中,应用程序主要包含两方面的内容:G ...
- DefaultTableCellRenderer 自定义
方式一: DefaultTableCellRenderer render = new DefaultTableCellRenderer(); render.setHorizontalAlignment ...