Skip to content

Commit 055e427

Browse files
committed
add mobx
1 parent 2148afe commit 055e427

8 files changed

Lines changed: 91 additions & 134 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
.vscode
32
dist
43
node_modules

4_mobx/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
[MobX](https://github.com/mobxjs/mobx)
1+
[MobX][1]
22

3+
[1]: https://github.com/mobxjs/mobx
4+
5+
a simple MobX [demo](https://react-sbgkk6.stackblitz.io)

4_mobx/src/app.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
3-
import { Provider } from 'react-redux'
4-
import configureStore from './store'
53

64
import App from 'containers/App'
75

8-
const store = configureStore()
6+
import { observable } from 'mobx'
7+
import { Provider, observer, inject } from 'mobx-react'
8+
9+
import stores from './store'
10+
11+
@inject('clickTimes')
12+
@observer
13+
class Counter extends React.Component {
14+
15+
constructor(props) {
16+
super(props)
17+
}
18+
19+
onInc = () => {
20+
this.props.clickTimes.inc()
21+
}
22+
23+
onDec = () => {
24+
this.props.clickTimes.dec()
25+
}
26+
27+
render() {
28+
return <div>
29+
<p>Count: {this.props.clickTimes.times}</p>
30+
<button onClick={this.onInc}> + </button>
31+
<button onClick={this.onDec}> - </button>
32+
</div>
33+
}
34+
}
935

1036
ReactDOM.render(
11-
<Provider store={store}>
12-
<App/>
37+
<Provider {...stores}>
38+
<Counter />
1339
</Provider>,
14-
document.getElementById('app')
15-
)
40+
document.getElementById('app')
41+
)
42+
43+
// ReactDOM.render(
44+
// <App/>
45+
// ,
46+
// document.getElementById('app')
47+
// )

4_mobx/src/containers/App/constants.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

4_mobx/src/containers/App/index.jsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import React from 'react'
2-
import { connect } from 'react-redux'
32
import Loading from 'components/Loading'
43
import List from 'components/List'
54

65
import './style.css'
76

8-
import {
9-
searchUsers,
10-
getFollowers,
11-
getFollowings
12-
} from './actions'
7+
// import {
8+
// searchUsers,
9+
// getFollowers,
10+
// getFollowings
11+
// } from './actions'
1312

1413
export default
15-
@connect(
16-
state => (
17-
{
18-
users: state.usersReducer,
19-
followers: state.followersReducer,
20-
followings: state.followingsReducer
21-
}
22-
)
23-
)
2414
class App extends React.Component {
2515
constructor(props) {
2616
super(props)

4_mobx/src/containers/App/reducers.js

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { observable, action } from 'mobx'
2+
3+
class clickTimesStore {
4+
@observable times = 0;
5+
@action inc = () => {
6+
this.times++
7+
}
8+
@action dec = () => {
9+
this.times--
10+
}
11+
}
12+
13+
const clickTimes = new clickTimesStore
14+
15+
export default clickTimes

4_mobx/src/store/index.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
import { createStore, applyMiddleware, combineReducers, compose } from 'redux'
2-
import thunk from 'redux-thunk'
3-
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
4-
import {
5-
usersReducer,
6-
followersReducer,
7-
followingsReducer
8-
} from 'containers/App/reducers'
1+
import clickTimes from './clickTimesStore'
92

10-
const reducer = combineReducers({
11-
usersReducer,
12-
followersReducer,
13-
followingsReducer
14-
})
3+
const stores = {
4+
clickTimes
5+
}
156

16-
const configureStore = () => createStore(
17-
reducer,
18-
composeEnhancers(
19-
applyMiddleware(thunk)
20-
)
21-
)
7+
export default stores
228

23-
export default configureStore
9+
// import { createStore, applyMiddleware, combineReducers, compose } from 'redux'
10+
// import thunk from 'redux-thunk'
11+
// const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
12+
// import {
13+
// usersReducer,
14+
// followersReducer,
15+
// followingsReducer
16+
// } from 'containers/App/reducers'
17+
18+
// const reducer = combineReducers({
19+
// usersReducer,
20+
// followersReducer,
21+
// followingsReducer
22+
// })
23+
24+
// const configureStore = () => createStore(
25+
// reducer,
26+
// composeEnhancers(
27+
// applyMiddleware(thunk)
28+
// )
29+
// )
30+
31+
// export default configureStore

0 commit comments

Comments
 (0)