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

226
Views
Adding properties on this vs adding properties on this.state. What's the difference?

I know functional components are easier to use than class components but I'm trying to learn doing things using both of them to understand better. So I looked up some examples at official react website.

The Timer example at reactjs.org/#a-stateful-component adds seconds as a state of component but it adds interval directly on as property of this.

I wonder if there is difference between both approaches? When should I add a property on this instead of adding it as a state?

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = { seconds: 0 }; //adding as state
  }

  tick() {
    this.setState(state => ({
      seconds: state.seconds + 1
    }));
  }

  componentDidMount() {
    this.interval = setInterval(() => this.tick(), 1000); //added property on this
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {
    return (
      <div>
        Seconds: {this.state.seconds}
      </div>
    );
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

General rule of thumb is. If you want something to change in UI. You update it in state if no UI change is required. You just update the property

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!