Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
JavaScript React setInterval "this" issue

I'm trying to learn React and I don't understand why this does not do anything when I try without an arrow function

componentDidMount() {
    var self = this
    this.timer = window.setInterval(function () {
        self.increment
    }, 1000)
}

Is the correct pattern self.increment()?

So why do I need the () when I don't need it in:

componentDidMount() {
    this.timer = window.setInterval(this.increment.bind(this),1000)
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not invoking self.increment, you are properly scoping this for your code to work. Maybe it appears it's not working because your interval's callback does not do anything :)

Also, here's a simpler version of your code, you could omit the .bind if your function does not need to access your component's scope, but typically in react you'd want access to that:

this.timer = setInterval(this.increment.bind(this), 1000)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!