• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

154
Views
javascript this in react function component event handler
const TimerView = observer(() => {
  const [timer] = useState(() =>
    observable({
      secondsPassed: 1,
      increaseTimer() {
        console.log(this, 888);
        this.secondsPassed++;
      },
    })
  );

  return (
  //   when click the span, error: this is undefined
  //   <span onClick={timer.increaseTimer}>
  //   Seconds: {timer.secondsPassed}
  //   </span>
  //  

    <span onClick={() => timer.increaseTimer()}>
      Seconds: {timer.secondsPassed}
    </span>

  //  this one works fine
  );
});

observer and observable are in mobx and mobx-react-lite. What's the difference between these two situation.It's so confused.

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error