简体   繁体   English

setInterval function 无箭头 function

[英]setInterval function without arrow function

I am learning about react components following the documentation https://facebook.github.io/react/docs/state-and-lifecycle.html我正在学习文档https://facebook.github.io/react/docs/state-and-lifecycle.html之后的反应组件

Why do we need to use arrow function here:为什么我们需要在这里使用箭头 function:

this.timerID = setInterval(() => this.tick(), 1000);

Why can't I just say (obviously it doesn't work)为什么我不能只说(显然它不起作用)

this.timerID = setInterval(this.tick(), 1000);

The first argument for setInterval is of type function . setInterval的第一个参数是function类型。 If you write this:如果你这样写:

this.timerID = setInterval(this.tick(), 1000);

…then you don't pass a function, instead you execute the function this.tick immediately and then pass the value returned by that function call as an argument. ...然后你不传递函数,而是立即执行函数this.tick然后传递该函数调用返回的值作为参数。

You could write it like this:可以这样写:

this.timerID = setInterval(this.tick, 1000);

If you omit the parentheses, you pass a reference to your this.tick function, which is then executed by setInterval after 1000 milliseconds.如果省略括号,则传递对this.tick函数的引用,然后在 1000 毫秒后由setInterval执行。

setInterval takes function as first argument, in the second case it is getting a returned value setInterval将函数作为第一个参数,在第二种情况下它获取返回值

Change it to将其更改为

this.timerID = setInterval(this.tick.bind(this), 1000);

or

 this.timerID = setInterval(() => this.tick(), 1000);

which is probably what you need when you want to bind the function to the React context.当您想将函数绑定到 React 上下文时,这可能是您所需要的。

See this answer on why you need to bind functions in React请参阅有关为什么需要在 React 中绑定函数的答案

If you don't you could have simply written如果你不这样做,你可以简单地写

this.timerID = setInterval(this.tick, 1000);

Why do we need to use arrow function here为什么这里需要使用箭头函数

Answer is simple : see the result inside live script example...答案很简单:查看实时脚本示例中的结果...

 class Persons{ scopeOne(){ // your will see the result this will display our current object 'Persons' setInterval(function(){ console.info(' SCOPE ONEo : '+this); },3000); } scopeTwo(){ setInterval(function(){ console.info(' SCOPE TWO : '+this); }.bind(this) ,3000); } scopeThree(){ setInterval(() => { console.info(' SCOPE THREE : '+this) },3000); } } let p = new Persons(); p.scopeOne(); p.scopeTwo(); p.scopeThree();

in first scope the result is WINDOW OBJECT so we cannot access our current class scope so in 2nd scope we using scope with bind(this) that helps to bind our current object scope, and in third which do same as 2nd scope calling the current object....在第一个范围中,结果是WINDOW OBJECT,因此我们无法访问我们当前的类范围,因此在第二个范围中,我们将范围与 bind(this) 一起使用,这有助于绑定我们当前的对象范围,在第三个范围中,它与调用当前对象的第二个范围相同....

Transform a function called tick into a variable with an anonymous function, so you can pass it as an argument without parentheses.将名为 Tick 的 function 转换为具有匿名 function 的变量,因此您可以将其作为不带括号的参数传递。

Taking this code ( https://codepen.io/gaearon/pen/amqdNr?editors=0010 ) as a base, you can update like this:以此代码( https://codepen.io/gaearon/pen/amqdNr?editors=0010 )为基础,您可以像这样更新:

 componentDidMount() {
    this.timerID = setInterval(this.tick,1000);
  }

  tick = () => {
    this.setState({
      date: new Date()
    });
  }

You need to supply a function reference, you are trying to invoke a function, unless that function returns a function reference, your code will not work您需要提供一个函数引用,您正在尝试调用一个函数,除非该函数返回一个函数引用,否则您的代码将无法工作

It should look like so它应该看起来像这样

this.tick = function() { .... }

this.timerID = setInterval(this.tick, 1000);

如果您没有使用箭头函数,那么您的代码应该如下所示:

this.timerID = setInterval(function(){ this.tick() }, 1000);

The simple answer to that is that the tick function needs to be able access the context of its use/ this , which is the Clock component.对此的简单回答是,tick 函数需要能够访问其 use/ this的上下文,也就是 Clock 组件。 In other words it needs to be bound to the Clock component so that it works in the context of the Clock component and not the global context which is everything outside of the Clock component.换句话说,它需要绑定到 Clock 组件,以便它在 Clock 组件的上下文中工作,而不是在 Clock 组件之外的全局上下文中工作。

Functions within React classes are not bound by default and failure to bind the function will return undefined instead of the expected result. React 类中的函数默认不绑定,绑定失败将返回undefined而不是预期的结果。

There are several ways to bind the tick function to the Clock component example from the Reactjs website.有几种方法可以将滴答函数绑定到 Reactjs 网站上的 Clock 组件示例。

  1. Arrow functions can access this which is why they are used in the example.箭头函数可以访问this ,这就是在示例中使用它们的原因。 In other words, writing an arrow function implicitly means it binds its contents to the local context (the Clock component in this case).换句话说,隐式编写箭头函数意味着它将其内容绑定到本地上下文(在本例中为 Clock 组件)。 Read more on that in this Medium article在这篇Medium 文章中阅读更多相关内容

  2. Bind the tick function in the constructor在构造函数中绑定tick函数

class Clock extends React.Component{
 constructor(props){
   super(props);
   this.state={date: new Date()}
   this.tick=this.tick.bind(this)
 }
  1. Bind the tick function in the setInterval method在setInterval方法中绑定tick函数
 componentDidMount() {
    this.timerID = setInterval(this.tick.bind(this),
      1000
    );
  }

从技术上讲,如果this.tick()返回一个函数,您应该能够使用第二个代码片段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM