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

74
Views
Cannot read properties of undefined - javascript class

I wanted to try mobx for react state management, however, I can't get this to work. I have this simple store for storing just a number called counter. You can increment/decrement it, and increse it by x. I have this file store.js for storing the state:

import { action, makeObservable, observable } from "mobx";


class CounterStore {
    counter = 0;

    constructor() {
        makeObservable(this, {
            counter: observable,
            increment: action,
            decrement: action,
            increase: action
        });
      
    }

    increment() {
        this.counter++;
    }

    decrement() {
        this.counter--;
    }

    increase(value) {
        this.counter = this.counter + value;
    }
}

const store = new CounterStore();

export default store;

And then I have this component named Counter inside Counter.js, which executes the methods like increment and decrement for the store

import { useRef } from "react";
import classes from './Counter.module.css'
import store from "../store";

function Counter() {

    const numberInput = useRef();

    const increaseHandler = (event) => {
        event.preventDefault(); 
        if (numberInput.current.value === ''){
            return;
        }
        store.increase(parseInt(numberInput.current.value))
    };
    return (
        <div>
            <div className={classes.counter}>{store.counter}</div>
            <div className={classes.row}>
                <button className={classes.btn} onClick={store.increment}>Increment</button>
                <button className={classes.btn} onClick={store.decrement}>Decrement</button>
            </div>
            <form className={classes.row} onSubmit={increaseHandler}>
                <button className={classes.btn}>Increase by x</button>
                <input className={classes.input} required="true " type="number" ref={numberInput} />
            </form>

        </div>
    );
}

export default Counter;

But when i press the button, I always get

Uncaught TypeError: Cannot read properties of undefined (reading 'counter') at increment

How can the counter possibly be undefined? I already export the instantiated store.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your function is losing context (this), you need to read about it to understand how it works. You destructured this function from the store and it is not bound to it anymore.

The most common way is to define all actions as arrow functions, like that:

    // Use arrow function to define methods
    increment = () => {
        this.counter++;
    }

    decrement = () => {
        this.counter--;
    }

    increase = (value) => {
        this.counter = this.counter + value;
    }

That way you don't need to worry about the context, it will be automatically bound to the current class.

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!