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'}} ...
随机推荐
- CLion提示can't find stdio.h等错误
先上解决办法,启动参数如下: $ LANG=en_US.UTF-8 /path/to/clion.sh 查了好知久,竟然就由于编码的原因.可是Ubuntu已经设置为英文UTF-8,还是可以通过上面的方 ...
- jquery 3D分页翻转滑块
jquery 3D分页翻转滑块,jquery分页,jquery插件,jquery,3D翻转,css3分页,360度旋转,网页特效代码3D分页翻转滑块是一款使用网格样式与滑块效果分页的特效.
- 夏日户外风景PSD素材
夏日户外风景PSD素材适用于向日葵素材背景图设计 地址:http://www.huiyi8.com/xiangrikui/
- Vue 数组中更新属性值后,视图不更新,等待其他元素更新后会触发的解决办法
因为 JavaScript 的限制,Vue.js 不能检测到下面数组变化: 直接用索引设置元素,如 vm.items[0] = {}: 修改数据的长度,如 vm.items.length = 0. t ...
- 常见css兼容问题
链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...
- JavaMail API的应用
JavaMail API 是一个用于阅读.编写和发送电子消息的可选包(标准扩展),用来创建邮件用户代理(Mail User Agent,MUA)类型程序. JavaMail API 需要 JavaBe ...
- 集训Day5
生活还得继续 bzoj3771 题面让我笑了很长时间 给出 n个物品,价值为别为Xi且各不相同,现在可以取1个.2个或3个,问每种价值和有几种情况? *顺序不同算一种 很傻逼的一个母函数+容斥,用A( ...
- ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
- 京东SDK模板卡盘效果实现代码
最近在做京东模板,因为是最新平台,好多功能都需要摸索,俺技术一般,摸索出一个简易的卡盘功能 ——————使用的是分类推荐模块哦! 本着共享的精神,俺将代码放到这儿了,各人请自便.(代码还不够完善, ...