Created
September 29, 2023 22:04
-
-
Save JonathanTurnock/b1e68647dea57af9010141b8cd16d8e0 to your computer and use it in GitHub Desktop.
Mobx simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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