Skip to content

Instantly share code, notes, and snippets.

@JonathanTurnock
Created September 29, 2023 22:04
Show Gist options
  • Save JonathanTurnock/b1e68647dea57af9010141b8cd16d8e0 to your computer and use it in GitHub Desktop.
Save JonathanTurnock/b1e68647dea57af9010141b8cd16d8e0 to your computer and use it in GitHub Desktop.
Mobx simple example
// App.js
import React from "react";
import { observer } from "mobx-react-lite";
import { counterStore } from "./store";
const App = observer(() => {
return (
<div>
<h1>{counterStore.count}</h1>
<button onClick={() => counterStore.increment()}>Increment</button>
<button onClick={() => counterStore.decrement()}>Decrement</button>
</div>
);
});
export default App;
// store.js
import { makeAutoObservable } from "mobx";
class CounterStore {
count = 0;
constructor() {
makeAutoObservable(this);
}
increment() {
this.count += 1;
}
decrement() {
this.count -= 1;
}
}
export const counterStore = new CounterStore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment