Skip to content

Commit ca3efeb

Browse files
authored
Merge pull request #1037 from gaearon/mobx-example
Mobx example
2 parents 7f60ce5 + a91a7c7 commit ca3efeb

File tree

8 files changed

+4297
-0
lines changed

8 files changed

+4297
-0
lines changed

examples/mobx/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["env", "react"],
3+
"plugins": ["react-hot-loader/babel", "transform-class-properties", "transform-decorators-legacy"]
4+
}

examples/mobx/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

examples/mobx/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "hot-mobx",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"start": "webpack-dev-server --hot"
7+
},
8+
"devDependencies": {
9+
"babel-core": "^6.26.0",
10+
"babel-loader": "^7.1.2",
11+
"babel-plugin-transform-class-properties": "^6.24.1",
12+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
13+
"babel-preset-env": "^1.6.1",
14+
"babel-preset-react": "^6.24.1",
15+
"html-webpack-plugin": "^2.30.1",
16+
"webpack": "^3.10.0",
17+
"webpack-dev-server": "^2.9.7"
18+
},
19+
"dependencies": {
20+
"autobind-decorator": "^2.1.0",
21+
"mobx": "^5.0.3",
22+
"mobx-react": "^5.2.3",
23+
"react": "^16.2.0",
24+
"react-dom": "^16.2.0",
25+
"react-hot-loader": "next"
26+
}
27+
}

examples/mobx/src/App.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import { hot, setConfig } from 'react-hot-loader'
3+
import Counter from './Counter'
4+
5+
const Element1 = ({ children }) => <div>Block1 {children}</div>
6+
7+
const Element2 = () => (
8+
<div>
9+
Block2 <Counter />
10+
</div>
11+
)
12+
13+
const App = () => (
14+
<h1>
15+
Hello, mobx<br />
16+
<Counter />
17+
<Element1>
18+
<Counter />
19+
</Element1>
20+
<Element2 />
21+
</h1>
22+
)
23+
24+
setConfig({ logLevel: 'debug' })
25+
26+
export default hot(module)(App)

examples/mobx/src/Counter.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React, { Component } from 'react'
2+
import { hot } from 'react-hot-loader'
3+
import { observer } from 'mobx-react'
4+
import { observable } from 'mobx'
5+
6+
class Counter extends React.Component {
7+
state = { count: Math.round(Math.random() * 1000) }
8+
9+
componentDidMount() {
10+
//this.interval = setInterval(this.increment, 200)
11+
}
12+
13+
componentWillUnmount() {
14+
clearInterval(this.interval)
15+
}
16+
17+
increment = () => {
18+
this.setState(prevState => ({ count: prevState.count + 1 }))
19+
}
20+
21+
render() {
22+
return this.state.count
23+
}
24+
}
25+
26+
class Comp extends Component {
27+
state = {
28+
obsObj: null,
29+
}
30+
31+
static getDerivedStateFromProps(nextProps, prevState) {
32+
return { obsObj: nextProps.obsObj }
33+
}
34+
35+
componentDidMount() {
36+
//setInterval(() => this.setState({update: 1}), 5000);
37+
}
38+
39+
render() {
40+
const { prop1 } = this.state.obsObj || {}
41+
return (
42+
<div style={{ border: '10px solid red' }}>
43+
{prop1}
44+
<Counter />
45+
</div>
46+
)
47+
}
48+
}
49+
50+
const ObsComp = observer(Comp)
51+
52+
class App extends Component {
53+
@observable
54+
obj = {
55+
prop1: 'Example',
56+
}
57+
58+
render() {
59+
return (
60+
<div>
61+
<ObsComp obsObj={this.obj} />
62+
</div>
63+
)
64+
}
65+
}
66+
67+
export default App

examples/mobx/src/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import { render } from 'react-dom'
3+
import App from './App'
4+
5+
const root = document.createElement('div')
6+
document.body.appendChild(root)
7+
8+
render(<App />, root)

examples/mobx/webpack.config.babel.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-disable */
2+
const path = require('path')
3+
const webpack = require('webpack')
4+
const HtmlWebpackPlugin = require('html-webpack-plugin')
5+
6+
module.exports = {
7+
entry: ['./src/index'],
8+
output: {
9+
path: path.join(__dirname, 'dist'),
10+
filename: 'bundle.js',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
exclude: /node_modules|packages/,
16+
test: /\.js$/,
17+
use: 'babel-loader',
18+
},
19+
],
20+
},
21+
resolve: {
22+
extensions: ['.ts', '.tsx', '.js', '.jsx'],
23+
alias: {
24+
react: path.resolve(path.join(__dirname, './node_modules/react')),
25+
'babel-core': path.resolve(
26+
path.join(__dirname, './node_modules/@babel/core'),
27+
),
28+
},
29+
},
30+
plugins: [new HtmlWebpackPlugin(), new webpack.NamedModulesPlugin()],
31+
}

0 commit comments

Comments
 (0)