在React中正确使用箭头函数

koj*_*ow7 34 javascript ecmascript-6 reactjs arrow-functions ecmascript-next

我正在使用ReactJS与Babel和Webpack以及使用ES6以及箭头函数的建议类字段.我理解箭头函数通过不重新创建每个渲染的函数来使事情更有效,类似于构造函数中的绑定方式.但是,如果我正确使用它们,我并不是100%确定.以下是我的代码在三个不同文件中的简化部分.

我的代码:

Main.js

prevItem = () => {
    console.log("Div is clicked")
}

render(){
    return (
         <SecondClass prevItem={this.prevItem} />
    )
}
Run Code Online (Sandbox Code Playgroud)

SecondClass.js

<ThirdClass type="prev" onClick={()=>this.props.prevItem()} />
Run Code Online (Sandbox Code Playgroud)

ThirdClass.js

<div onClick={()=>{this.props.onClick()}}>Previous</div>
Run Code Online (Sandbox Code Playgroud)

题:

上面的代码是否正确使用箭头功能?我注意到对于SecondClass.js我也可以使用:

<ThirdClass type="prev" onClick={this.props.prevItem} />
Run Code Online (Sandbox Code Playgroud)

由于我在原始函数定义中使用了ES6箭头函数,因此一种方法或另一种方法之间是否存在差异?或者我应该一直使用箭头语法直到我的最后一个div?

Sag*_*b.g 48

据我所知,箭头函数通过不在每次引用时重新创建函数来提高效率

这是不是真的.

箭头函数this以词法方式处理上下文,其中"普通"函数动态地执行.

在内联箭头函数的两个示例中,您将在每个示例上创建一个新的函数实例render.
这将在每个渲染上创建并传递一个新实例

onClick={() => {}}
Run Code Online (Sandbox Code Playgroud)

在第3个示例中,您只有一个实例.
这仅传递对已存在实例的引用

onClick={this.myHandler}
Run Code Online (Sandbox Code Playgroud)


至于箭头函数作为类字段的好处(有一个小的缺点,我会将它发布在答案的底部),如果你有一个普通的函数处理程序,需要访问当前的classvia 实例this:

myHandler(){
  //  this.setState(...)
}
Run Code Online (Sandbox Code Playgroud)

你需要明确bindclass.
最常见的方法是在constructor它中运行,因为它只运行一次:

constructor(props){
  super(props);
  this.myHandler = this.myHandler.bind(this);
}
Run Code Online (Sandbox Code Playgroud)

如果你使用箭头函数作为处理程序,你不需要bind它,class因为如上所述,箭头函数使用词法上下文this:

myHandler = () => {
  //  this.setState(...)
}
Run Code Online (Sandbox Code Playgroud)

使用这两种方法,您将使用这样的处理程序:

<div onClick={this.myHandler}></div> 
Run Code Online (Sandbox Code Playgroud)

采用这种方法的主要原因:

<div onClick={() => this.myHandler(someParameter)}></div>
Run Code Online (Sandbox Code Playgroud)

是否要将参数传递给传递的本机旁边的处理程序event,这意味着您希望向上传递参数.

如上所述,这将在每个渲染上创建一个新的函数实例.
(有一个更好的方法,继续阅读).

针对此类用例运行示例:

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [{ name: 'item 1', active: false }, { name: 'item 2', active: true }],
    }
  }
  toggleITem = (itemName) => {
    this.setState(prev => {
      const nextState = prev.items.map(item => {
        if (item.name !== itemName) return item;
        return {
          ...item,
          active: !item.active
        }
      });
      return { items: nextState };
    });
  }
  render() {
    const { items } = this.state;
    return (
      <div>
        {
          items.map(item => {
            const style = { color: item.active ? 'green' : 'red' };
            return (
              <div
                onClick={() => this.toggleITem(item.name)}
                style={style}
              >
                {item.name}
              </div>
          
          )})
        }
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Run Code Online (Sandbox Code Playgroud)

更好的方法是创建组件组合.
您可以创建一个包装相关标记的子组件,将拥有它自己的处理程序,datahandler从父级获取和作为道具.

然后子组件将调用它从父组件获取的处理程序,并将其data作为参数传递.

使用子组件运行示例:

class Item extends React.Component {
  onClick = () => {
    const { onClick, name } = this.props;
    onClick(name);
  }
  render() {
    const { name, active } = this.props;
    const style = { color: active ? 'green' : 'red' };
    return (<div style={style} onClick={this.onClick}>{name}</div>)
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [{ name: 'item 1', active: false }, { name: 'item 2', active: true }],
    }
  }
  toggleITem = (itemName) => {
    this.setState(prev => {
      const nextState = prev.items.map(item => {
        if (item.name !== itemName) return item;
        return {
          ...item,
          active: !item.active
        }
      });
      return { items: nextState };
    });
  }
  render() {
    const { items } = this.state;
    return (
      <div>
        {
          items.map(item => {
            return <Item {...item} onClick={this.toggleITem} />
          })
        }
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Run Code Online (Sandbox Code Playgroud)

类别字段向下:
正如我所提到的,类字段有一个小的缺点.
一个类的方法和一类字段之间的区别是,类字段被附接到instance所述的class(构造函数).
类方法和对象附加到原型的位置.

因此,如果您将拥有大量此类的实例,您可能会受到性能影响.

鉴于此代码块:

class MyClass {
  myMethod(){}  
  myOtherMethod = () => {}
}
Run Code Online (Sandbox Code Playgroud)

巴贝尔会将其转化为:

var _createClass = function() {
  function defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor) descriptor.writable = true;
      Object.defineProperty(target, descriptor.key, descriptor);
    }
  }
  return function(Constructor, protoProps, staticProps) {
    if (protoProps) defineProperties(Constructor.prototype, protoProps);
    if (staticProps) defineProperties(Constructor, staticProps);
    return Constructor;
  };
}();

function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var MyClass = function() {
  function MyClass() {
    _classCallCheck(this, MyClass);

    this.myOtherMethod = function() {};
  }

  _createClass(MyClass, [{
    key: "myMethod",
    value: function myMethod() {}
  }]);

  return MyClass;
}();
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案.如此详细. (8认同)

Shu*_*tri 34

我理解箭头函数通过不重新创建每个渲染的函数来使事情更有效,类似于构造函数中的绑定方式.

这不是真的.这取决于您使用Arrow功能的确切位置.如果Arrow function在render方法中使用,那么它们会创建一个新的实例everytime渲染,就像调用它一样bind.考虑这个例子

<div onClick={()=>{this.onClick()}}>Previous</div>
Run Code Online (Sandbox Code Playgroud)

这里每次渲染时都会调用一个匿名函数,并在调用时调用该函数this.onClick.

但请考虑以下情况

onClick = () => {
    console.log("Div is clicked")
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,箭头函数不是每次都重新创建函数,而是在An arrow function does not have its own this; the this value of the enclosing execution context is used. 实例化类时将上下文绑定到React组件.这与如何相似binding works is constructor.这是其中的一部分,proposed class fields for arrow functions它不是ES6功能,

要了解您想要问的内容,您必须知道函数从其调用的位置获取其上下文.检查this question更多的理解.

在您的情况下,您已经习惯Arrow function定义prevItem,因此它获取了封闭的React组件的上下文.

prevItem = () => {
    console.log("Div is clicked")
}

render(){
    return (
         <SecondClass prevItem={this.prevItem} />
    )
}
Run Code Online (Sandbox Code Playgroud)

现在在它的子节点中,即使你prevItem使用任何自定义上下文调用using bind or arrow function,prevItem当在父节点中执行时,Main.js也将获得其封闭的React组件的上下文.因为你只是希望执行prevItem函数而不想从孩子那里传递任何数据,所以写作

<ThirdClass type="prev" onClick={()=>this.props.prevItem()} />
Run Code Online (Sandbox Code Playgroud)

<div onClick={()=>{this.props.onClick()}}>Previous</div>
Run Code Online (Sandbox Code Playgroud)

根本没用而且由于新的功能在创建只会增加性能含义SecondClassThirdClass每次.您根本不需要将这些函数定义为箭头函数,只需编写即可

<ThirdClass type="prev" onClick={this.props.prevItem} />
Run Code Online (Sandbox Code Playgroud)

<div onClick={this.props.onClick}>Previous</div>
Run Code Online (Sandbox Code Playgroud)

因为它已经在父母中绑定了.

现在,即使你必须从ThirdClass和SecondClass向这些函数传递一些额外的数据,你也不应该直接使用Arrow functionbind in render.看看这个答案How to Avoid binding in Render method

  • 使用箭头功能本身还不错。即使重新创建箭头功能,也可以在`render`中使用箭头功能。在大多数应用中,性能差异不会很明显。 (2认同)

sim*_*lor 6

所以你的第一种方法

<ThirdClass type="prev" onClick={()=>this.props.prevItem()} />
Run Code Online (Sandbox Code Playgroud)

在此方法中,您可以将ThirdClass中可用的任何参数传递给prevItem函数。这是用参数调用父函数的好方法。

<ThirdClass type="prev" onClick={()=>this.props.prevItem(firstArgument, secondArgument)} />
Run Code Online (Sandbox Code Playgroud)

您的第二种方法是

<ThirdClass type="prev" onClick={this.props.prevItem} />
Run Code Online (Sandbox Code Playgroud)

这种方法不允许您传递任何ThirdClass特定的参数。

两种方法都是正确的,仅取决于您的用例。两种使用es6箭头功能的方法都适用于上述相应场景


Ame*_*icA 5

使用JavaScriptcurring函数声明,可以是其他答案的不同方法,请注意以下代码:

clickHandler = someData => e => this.setState({
  stateKey: someData
});
Run Code Online (Sandbox Code Playgroud)

现在JSX,您可以编写:

<div onClick={this.clickHandler('someData')} />
Run Code Online (Sandbox Code Playgroud)

clickHandlersomeData带有返回的函数e的参数,但它没有内部使用clickHandler功能。所以效果很好。

为了更完整地编写,请如下编写:

clickHandler = someData => () => this.setState({
  stateKey: someData
});
Run Code Online (Sandbox Code Playgroud)

不需要e,所以为什么要写它。