score:2

Accepted answer
class SampleStore {
  counter = 0;

  constructor(arg) {
    makeAutoObservable(this);
  }

  // Make it arrow function
  increment = () => {
    this.counter++;
    return this.counter;
  }
  
  // Make it arrow function
  decrement = () => {
    this.counter--;
    return this.counter;
  }
}

score:0

import { useStore } from "../stores";
import { observer } from "mobx-react";

const Index = (props) => {
  const {sampleStore:store} = useStore();

  return (
    <>
      <h1>MobX and React.js example</h1>
      <p>{store.counter}</p>
      <button onClick={store.increment.bind(store)}>+</button>
      <button onClick={store.decrement.bind(store)}>-</button>
    </>
  );
};

export default observer(Index);

score:2

constructor(arg) {
  makeAutoObservable(this, {}, { autoBind: true });
}

Related Query

More Query from same tag