Skip to content

Instantly share code, notes, and snippets.

@mastermoo
Last active September 25, 2024 06:51
Show Gist options
  • Save mastermoo/740f5e83c60f32ea366595ec0a88ecdc to your computer and use it in GitHub Desktop.
Save mastermoo/740f5e83c60f32ea366595ec0a88ecdc to your computer and use it in GitHub Desktop.
counter example using mobx
// store.js
import {observable} from 'mobx';
class CounterStore {
@observable counter = 0;
increment() { this.counter++; }
decrement() { this.counter--; }
}
export default new CounterStore();
// MyComponent.js
import React from 'react';
import {observer} from 'mobx-react';
import store from './store.js';
const Counter = () => (
<div>
<button onClick={() => store.increment()}>+1</button>
<span>{store.counter}</span>
<button onClick={() => store.decrement()}>-1</button>
</div>
);
export default observer(Counter);
@loudsven
Copy link

loudsven commented Sep 24, 2024

Why there is no accessor keyword?
Documentation says:

observable annotation should always be used in combination with the accessor keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment