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'}} ...
随机推荐
- Git core objects
Git core objects Core objects in git blob object tree object commit object Git low level commands gi ...
- Window7 环境下 MariaDB 的安装 及使用
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方 ...
- 修改织梦DedeCms文章页默认title的方法
<title>{dede:field.title/}_{dede:field name='typeid' runphp='yes'}$id=@me;global $dsql;$sql=&q ...
- 世界各国Google网址大全
http://www.oschina.net/question/100896_50293 冰岛 https://www.google.is/ 丹麦 https://www.google.dk/ 挪威 ...
- X-Forward-For ip
用 Firefox 的Moify Headers 插件 服务器重新配置X-Forward-For 为正确的值. 如对典型的nginx + php fastcgi 环境( nginx 与 php fas ...
- Eclipse_插件_02_jd-eclipse插件的安装
1.去官网下载 jd-eclipse插件 2.解压后的文件夹A放到eclipse的drops文件夹下 3.删掉多余文件,确保文件夹A下只有plugin 和 freature 两个文件夹 4.清空osg ...
- Linux-NoSQL之memcached
1.memcached安装 yum search memcached yum install -y libevent memcached libmemcached 启动:/etc/init.d/mem ...
- 第六章-jQuery
jQuery的理念是: 写更少的代码, 完成更多的工作 jQuery有两个版本1.x和2.x, 版本2.x不再支持IE678 jQuery最明显的标志就是$, jQuery把所有的功能都封装在了jQu ...
- Ubuntu下安装deb包命令
原文地址:http://www.xitongzhijia.net/xtjc/20150206/37464.html 1.下载需要安装的deb包,输入以下命令安装: sudo dpkg -i packa ...
- About getByClass
不能获取class为多个的情况 function getByClass(parent,cls){ var res=[]; var ele=parent.getElementsByTagName(&qu ...