当咱们想起箭头函数时,脑海里可能会浮现
棒
,酷
,简洁
,有趣
等形容词,其实,咱们存在一些更充分的理由
使咱们在联想起箭头函数
时不得不想到的react
this
引发的问题箭头函数不会在函数体内从新定义 this
的值,这使得在回调中的行为更容易预测,而且避免了 this
在回调中潜存的 bug
浏览器
下面咱们来看一个 example
app
咱们指望点击按钮,改变按钮颜色,代码以下函数
class BrokenButton extends React.Component { render() { return ( <button onClick={this.handleClick} style={this.state}> Set background to red </button> ); } handleClick() { this.setState({ backgroundColor: "red" }); } } render(<BrokenButton />, document.getElementById("root"));
然而,当咱们点击按钮时,什么效果都没有,为何会这样呢性能
其实,不是 handleClick
方法没有起做用,由于 JavaScript
中压根没有方法, JavaScript
中只有函数,而函数中的 this
存在一些规则,正是这些规则,让上面的 handleClick
中的 this
值变成了 null
优化
你须要清楚明白的是: 你没法肯定一个方法函数中 this 的指向,由于它的值跟函数的调用方式有关
this
除非,你使用 箭头函数
,由于箭头函数中 this
的值是继承自 外围做用域
spa
class Button extends React.Component { render() { return ( <button onClick={() => this.setState({ backgroundColor: "red" })} style={this.state} > Set background to red </button> ); } } render(<Button />, document.getElementById("root"));
如今就对了,接下来,咱们继续调试
浏览器对 箭头函数
的支持大概是 73%
,由于目前,IE
并不支持。但若是你已经意识到这一点,而且你还会代码转译
,这对你来讲就不算什么问题code
你们都发现了,箭头函数
书写起来是很是容易的,但书写忒多的函数,也会形成一些问题
定义函数是昂贵的
浏览器每执行一次 =>
,就须要建立一个 新的函数对象
,这实际上是一个比较 昂贵
的操做
固然,若是你不是想构建一个 性能超级无敌宇宙螺旋棒
的组件,渲染一个 很是长
的列表或 很是大
的表格,你也不会发现这是一个 问题
因此,若是你的组件只是在页面中渲染个几回,你也 不必忒担忧
性能这方面的问题
两个相同的箭头函数并不相等
为了让你们意识到这个问题,接下来,咱们用 ==
比较一下两个相同的箭头函数相不相等
const a = x => x, b = x => x; render( <div> <h3> Are <code>a</code> and <code>b</code> equal by <code>==</code>? </h3> <p> {a == b ? "Yes!" : "No :("} </p> </div>, document.getElementById("root") );
若是你在 render
中使用箭头函数,那么你在每次调用 render
时都会去建立一个新的函数对象,此时,即便使用 PureComponent
和 shouldComponentUpdate
也起不到优化做用
你能够在下面实例中看清这一点,其中,<PropChangeCounter />
组件用于打印 props
改变的次数
import PropChangeCounter from "react-armory-prop-change-counter"; class App extends React.Component { constructor(props) { super(props); this.state = { email: "" }; } render() { return ( <div> <input placeholder="Email" value={this.state.email} onChange={e => this.setState({ email: e.target.value })} /> <PropChangeCounter constant={"this doesn't change"} value={this.state.email} onChange={e => this.setState({ email: e.target.value })} /> </div> ); } } render(<App />, document.getElementById("root"));
只定义一次
若是你以为 性能
对你的组件很重要,那么你确定会想若是在组件中只定义箭头函数 一次
该有多好
其中一种实现方式是在 constructor
中使用箭头函数,固然,对于复杂些的组价来讲,这会变的很笨拙
若是你使用了 Babel
或 create-react-app
构建你的应用,你能够将箭头函数设置为 class fields
或 arrow function methods
以下,你能够将 handleClick
从新定义为一个 arrow function method
,来修复第一个 example
中的 bug
class Button extends React.Component { render() { return ( <button onClick={this.handleClick} style={this.state}> Set background to red </button> ); } // Note: this syntax is not yet part of JavaScript proper, but is slated // for inclusion in the next version. It should already work with Babel. handleClick = () => { this.setState({ backgroundColor: "red" }); }; }
若是 环境支持
箭头函数,那么鼓励使用
尽可能避免对 React 组件
使用箭头函数,它会使 调试
变的困难
若是有须要,能够在 render
中使用箭头函数
为 性能
着想,避免在 render
中使用大量函数