Skip to content

Commit

Permalink
chore(examples): use mobx v6 in using-mobx example (#34351)
Browse files Browse the repository at this point in the history
  • Loading branch information
iChenLei authored Jan 3, 2022
1 parent 63207a2 commit 3341eea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
21 changes: 6 additions & 15 deletions examples/using-mobx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"src/**/*.js\"",
"lint": "eslint **/*.{js,jsx} --quiet -o linterrors.txt --ignore-path .gitignore"
"format": "prettier --write \"src/**/*.js\""
},
"keywords": [
"gatsby",
Expand All @@ -17,21 +16,13 @@
"license": "MIT",
"dependencies": {
"gatsby": "next",
"mobx": "^5.15.7",
"mobx-react": "^6.3.0",
"prop-types": "^15.7.2",
"react": "^16.9.0",
"react-dom": "^16.9.0"
"mobx": "^6.3.10",
"mobx-react": "^7.2.1",
"prop-types": "^15.8.0",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"prettier": "2.1.1"
}
}
17 changes: 9 additions & 8 deletions examples/using-mobx/src/components/Counter.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React, { Fragment } from "react"
import { observer, inject } from "mobx-react"
import React, { useContext } from "react"
import { MobXProviderContext, observer } from "mobx-react"

const Counter = inject(`store`)(
observer(({ store }) => (
<Fragment>
const Counter = observer(() => {
const store = useContext(MobXProviderContext)
return (
<>
<div>Counted to: {store.Count}</div>
<div>
<button onClick={() => store.Increment()}>Add</button>
<button onClick={() => store.Decrement()}>Subtract</button>
</div>
</Fragment>
))
)
</>
)
})

export default Counter
16 changes: 10 additions & 6 deletions examples/using-mobx/src/models/CounterModel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { observable, action, decorate } from "mobx"
import { observable, action, makeObservable } from "mobx"

class CounterModel {
Count = 0

constructor() {
makeObservable(this, {
Count: observable,
Increment: action.bound,
Decrement: action.bound,
})
}

Increment() {
this.Count += 1
}
Expand All @@ -11,10 +19,6 @@ class CounterModel {
this.Count -= 1
}
}
decorate(CounterModel, {
Count: observable,
Increment: action,
Decrement: action,
})

const CounterStore = new CounterModel()
export default CounterStore
12 changes: 8 additions & 4 deletions examples/using-mobx/wrap-with-provider.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from "react"
import { Provider } from "mobx-react"
import { enableStaticRendering, MobXProviderContext } from "mobx-react"
import CounterStore from "./src/models/CounterModel"

// eslint-disable-next-line react/display-name,react/prop-types
export default ({ element }) => (
<Provider store={CounterStore}>{element}</Provider>
// https://mobx.js.org/react-integration.html#static-rendering
enableStaticRendering(true)

const App =({ element }) => (
<MobXProviderContext.Provider value={CounterStore}>{element}</MobXProviderContext.Provider>
)

export default App;

0 comments on commit 3341eea

Please sign in to comment.