前端之家收集整理的这篇文章主要介绍了
React类,方法绑定(第三部分),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
403_0@这是React和ECMAScript6/ECMAScript7结合使用系列
文章的第三篇。
@H_
403_0@下面是所有系列
文章章节的
链接:
@H_403_0@本篇文章Github源码
@H_
403_0@你在看
上一篇文章中的
CartItem render 方法
中使用
{this.increaseQty.bind(this)}
代码时,你肯定会觉得困惑。
@H_
403_0@如果我们尝试将
{this.increaseQty.bind(this)}
代码替换成
{this.increaseQty}
,我们将在浏览器控制台中发现如下
错误:
Uncaught TypeError: Cannot read property 'setState' of undefined
.
@H_
403_0@
@H_
403_0@这是因为我们当我们
调用一个用那种
方法绑定的
方法时,
this
不是类它本身,
this
未定义。相反,如果在案例中,
React.createClass()
所有的
方法将会
自动绑定对象的实例。这可能是一些开发者的直觉。
@H_
403_0@No autobinding was the decision of React team when they implemented support of ES6 classes for React components. You could find out more about reasons for doing so in this blog post.
@H_
403_0@当
React team
通过
ES6
来实现对React 组建的
支持时,没有设置
自动绑定是
React team
的一个决定。在
这篇博客中你能发现更多关于为什么这么处理的原因。
@H_
403_0@现在让我们来看看在你的JSX案例中,使用ES6类创建的
方法的多种
调用方法。我所考虑到的多种不同的
方法都在
这个GitHub repository。
Method 1. 使用Function.prototype.bind().
@H_
403_0@之前我们已经见到过:
export default class CartItem extends React.Component {
render() {
<button onClick={this.increaseQty.bind(this)} className="button success">+</button>
}
}
@H_
403_0@当任何ES6的常规
方法在
方法原型中进行
this
绑定时,this指向的是类实例。如果你想了解更多
Function.prototype.bind()
,你可以访问
这篇MDN博客
Method 2. 在构造函数中定义函数
@H_
403_0@此
方法是上一个与类构造
函数的
用法的混合:
export default class CartItem extends React.Component {
constructor(props) {
super(props);
this.increaseQty = this.increaseQty.bind(this);
}
render() {
<button onClick={this.increaseQty} className="button success">+</button>
}
}
@H_
403_0@你不要再次在JSX
代码的其它地方进行绑定,但是这将
增加构造
函数中的
代码量。
Method 3. 使用箭头函数和构造函数
@H_
403_0@
ES6 fat arrow functions preserve this context when they are called. We could use this feature and redefine increaseQty() inside constructor in the following way:
@H_
403_0@当
方法被
调用时,
ES6 fat arrow functions会保留上下文。我们能使用这个特征用下面的
方法在构造
函数中重定义
increaseQty()
函数。
export default class CartItem extends React.Component {
constructor(props) {
super(props);
this._increaseQty = () => this.increaseQty();
}
render() {
<button onClick={_this.increaseQty} className="button success">+</button>
}
}
Method 4. 使用箭头函数和ES2015+类属性
@H_
403_0@另外,你可以使用ES2015+类
属性语法和箭头
函数:
export default class CartItem extends React.Component {
increaseQty = () => this.increaseQty();
render() {
<button onClick={this.increaseQty} className="button success">+</button>
}
}
@H_
403_0@
属性初始化和Method 3中的
功能一样。
@H_
403_0@
警告: 类
属性还不是当前JavaScript标准的一部分。但是你可以在Babel中自由的使用他们。你可以在
这篇文章中了解更多类
属性的使用。
Method 5. 使用 ES2015+ 函数绑定语法.
@H_
403_0@最近,Babel介绍了
Function.prototype.bind()
中
::
的使用。在这里我就不深入细节讲解它工作的原理。有很多其它的小伙伴已经有很完美的解释。你可以Google
搜索blog 2015 function bind
了解更多细节。
@H_
403_0@下面是
ES2015+
函数绑定的使用:
export default class CartItem extends React.Component {
constructor(props) {
super(props);
this.increaseQty = ::this.increaseQty;
// line above is an equivalent to this.increaseQty = this.increaseQty.bind(this);
}
render() {
<button onClick={this.increaseQty} className="button success">+</button>
}
}
@H_
403_0@强调:这个特性是高度的实验,使用它你得自己承担风险。
Method 6. 在调用方法的方使用 ES2015+ 函数绑定语法.
@H_
403_0@You also have a possibility to use ES2015+ function bind
Syntax directly in your JSX without touching constructor. It will look like:
@H_
403_0@你也可以直接在非构造
函数里面的JSX里面直接使用ES2015+
函数绑定。
效果如下:
export default class CartItem extends React.Component {
render() {
<button onClick={::this.increaseQty} className="button success">+</button>
}
}
@H_
403_0@非常简洁,唯一的缺点是,这个
函数将在每个后续渲染组件上重新创建。这不是最佳的。更重要的是,如果你使用像PureRenderMixin的东西将会出现。
结论
@H_
403_0@在这篇
文章中,我们已经发现了多种React组建类
方法的绑定
方法。
参考文档