在 React 中什么时候使用箭头函数

前端之家收集整理的这篇文章主要介绍了在 React 中什么时候使用箭头函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我们想起箭头函数时,脑海里可能会浮现 简洁有趣 等形容词,其实,我们存在一些 更充分的理由 使我们在联想起 箭头函数 时不得不想到的

解决 this 引起的问题

箭头函数不会在函数体内重新定义 this 的值,这使得在回调中的行为更容易预测,并且避免了 this 在回调中潜存的 bug

下面我们来看一个 example

我们期望点击按钮,改变按钮颜色,代码如下

  1. class BrokenButton extends React.Component {
  2. render() {
  3. return (
  4. <button onClick={this.handleClick} style={this.state}>
  5. Set background to red
  6. </button>
  7. );
  8. }
  9.  
  10. handleClick() {
  11. this.setState({ backgroundColor: "red" });
  12. }
  13. }
  14.  
  15. render(<BrokenButton />,document.getElementById("root"));

然而,当我们点击按钮时,什么效果都没有,为什么会这样呢

其实,不是 handleClick 方法没有起作用,因为 JavaScript 中压根没有方法JavaScript 中只有函数,而函数中的 this 存在一些规则,正是这些规则,让上面的 handleClick 中的 this 值变成了 null

你需要清楚明白的是: 你无法确定一个方法函数中 this 的指向,因为它的值跟函数调用方式有关

除非,你使用 箭头函数,因为箭头函数this 的值是继承自 外围作用域

  1. class Button extends React.Component {
  2. render() {
  3. return (
  4. <button
  5. onClick={() => this.setState({ backgroundColor: "red" })}
  6. style={this.state}
  7. >
  8. Set background to red
  9. </button>
  10. );
  11. }
  12. }
  13.  
  14. render(<Button />,document.getElementById("root"));

现在就对了,接下来,我们继续

浏览器支持

浏览器对 箭头函数支持大概是 73%,因为目前,IE不支持。但如果你已经意识到这一点,并且你还会代码转译,这对你来说就不算什么问题

性能问题

大家都发现了,箭头函数 书写起来是非常容易的,但书写忒多的函数,也会造成一些问题

定义函数是昂贵的

浏览器每执行一次 =>,就需要创建一个 新的函数对象,这其实是一个比较 昂贵 的操作

当然,如果你不是想构建一个 性能超级无敌宇宙螺旋棒 的组件,渲染一个 非常长 的列表或 非常大 的表格,你也不会发现这是一个 问题

所以,如果你的组件只是在页面中渲染个几次,你也 没必要忒担心 性能这方面的问题

两个相同的箭头函数并不相等

为了让大家意识到这个问题,接下来,我们用 == 比较一下两个相同的箭头函数相不相等

  1. const a = x => x,b = x => x;
  2.  
  3. render(
  4. <div>
  5. <h3>
  6. Are <code>a</code> and <code>b</code> equal by <code>==</code>?
  7. </h3>
  8. <p>
  9. {a == b ? "Yes!" : "No :("}
  10. </p>
  11. </div>,document.getElementById("root")
  12. );

如果你在 render 中使用箭头函数,那么你在每次调用 render 时都会去创建一个新的函数对象,此时,即使使用 PureComponentshouldComponentUpdate 也起不到优化作用

你可以在下面实例中看清这一点,其中,<PropChangeCounter /> 组件用于打印 props 改变的次数

  1. import PropChangeCounter from "react-armory-prop-change-counter";
  2.  
  3. class App extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = { email: "" };
  7. }
  8. render() {
  9. return (
  10. <div>
  11. <input
  12. placeholder="Email"
  13. value={this.state.email}
  14. onChange={e => this.setState({ email: e.target.value })}
  15. />
  16. <PropChangeCounter
  17. constant={"this doesn't change"}
  18. value={this.state.email}
  19. onChange={e => this.setState({ email: e.target.value })}
  20. />
  21. </div>
  22. );
  23. }
  24. }
  25.  
  26. render(<App />,document.getElementById("root"));

只定义一次

如果你觉得 性能 对你的组件很重要,那么你肯定会想如果在组件中只定义箭头函数 一次 该有多好

其中一种实现方式是在 constructor 中使用箭头函数,当然,对于复杂些的组价来说,这会变的很笨拙

如果你使用了 Babelcreate-react-app 构建你的应用,你可以将箭头函数设置为 class fieldsarrow function methods

如下,你可以将 handleClick 重新定义为一个 arrow function method,来修复第一个 example 中的 bug

  1. class Button extends React.Component {
  2. render() {
  3. return (
  4. <button onClick={this.handleClick} style={this.state}>
  5. Set background to red
  6. </button>
  7. );
  8. }
  9.  
  10. // Note: this Syntax is not yet part of JavaScript proper,but is slated
  11. // for inclusion in the next version. It should already work with Babel.
  12. handleClick = () => {
  13. this.setState({ backgroundColor: "red" });
  14. };
  15. }

总结

  • 如果 环境支持 箭头函数,那么鼓励使用

  • 尽量避免对 React 组件 使用箭头函数,它会使 调试 变的困难

  • 如果有需要,可以在 render 中使用箭头函数

  • 性能 着想,避免在 render 中使用大量函数

原文链接When should I use Arrow Functions? (James K Nelson)

猜你在找的React相关文章