简体   繁体   English

React:为什么我们不需要将上下文绑定到用作事件侦听器的箭头函数?

[英]React: Why do we not need to bind context to arrow functions being used as event listeners?

Looking at the react tutorial it explains that when we set event listeners, we generally have to bind the functions context, like is done here查看 react 教程,它解释说,当我们设置事件侦听器时,我们通常必须绑定函数上下文,就像在这里所做的那样

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState(state => ({
      isToggleOn: !state.isToggleOn
    }));
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

I understand this, because unless we bind context to the this.handleClick function we have no guarantee of what value this will actually hold when we call this.setState within the handleClick function.我理解这一点,因为除非我们将上下文绑定到this.handleClick function,否则当我们在 handleClick function 中调用this.setState时,我们无法保证this实际持有的值。 However, the tutorial goes on to state that we would not have to bind context to the handleClick function if we replaced <button onClick={this.handleClick}> with <button onClick={() => this.handleClick}>但是,教程继续到 state,如果我们将<button onClick={this.handleClick}>替换为<button onClick={() => this.handleClick}> ,我们将不必将上下文绑定到 handleClick function

How come the correct this context is automatically bound when we use an arrow function?当我们使用箭头 function 时,如何自动绑定正确this上下文?

You don't need to but arrow functions keep context of this as the object that defined them where traditional function context this as the object that calls them.您不需要箭头函数将其上下文保留为this它们的 object,而传统的 function 上下文this其保留为调用它们的 object。 You can use normal functions as long as they don't call this or keep a reference to this with a variable link const self = this and use self instead of this.您可以使用普通函数,只要它们不调用 this 或通过变量链接const self = this保持对 this 的引用,并使用 self 而不是 this。

Why is this the case?为什么会这样? Because they designed it to be so.因为他们设计的就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么以及何时需要在 React 中绑定函数和事件处理程序? - Why and when do we need to bind functions and eventHandlers in React? 为什么我们需要在react中绑定一个事件处理程序 - Why do we need to bind a event handler in react React Native:如果你使用箭头函数,是否需要使用 bind(this)? - React Native: if you use arrow functions, is there ever a need to use bind(this)? 如何添加和删除作为箭头函数的事件侦听器 - How to add and remove event listeners that are arrow functions 在 React 中,为什么我们在箭头 function 中调用它? - In React, why do we call this in arrow function? 为什么我们需要在React Native中绑定函数? - Why we need to bind function in React Native? react中的事件处理:在什么情况下我们需要在事件上使用箭头function,什么时候我们只需要通过class方法(函数)? - Event handling in react: in which cases do we need to use arrow function on event, and when do we just need to pass class method(function)? 为什么我们需要在React中进行默认导出? - Why do we need default export in React? 反应-箭头函数vs bind()和带参数的事件 - React - Arrow function vs bind() and event with argument 事件侦听器内部使用的异步函数是否会受到事件本身的限制? - Are async functions that are used inside event listeners bottlenecked by the event themselves?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM