File tree Expand file tree Collapse file tree
Cute-React/MobX-Learning/demo Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // cnpm i react react-dom prop-types mobx-react -S
2+ // cnpm i babel-preset-react -D
3+
4+ import React , { Component } from 'react' ;
5+ import ReactDOM from 'react-dom' ;
6+ import { observable , action } from 'mobx' ;
7+ import PropTypes from 'prop-types' ;
8+ import { observer , PropTypes as ObservablePropTypes } from 'mobx-react' ;
9+
10+
11+ class Store {
12+ @observable cache = { queue : [ ] }
13+
14+ @action . bound refresh ( ) {
15+ this . cache . queue . push ( 1 )
16+ }
17+ }
18+
19+ const store = new Store ( )
20+
21+
22+
23+ @observer
24+ class Bar extends Component {
25+ static propTypes = {
26+ queue : ObservablePropTypes . observableArray
27+ }
28+ render ( ) {
29+ const queue = this . props . queue ;
30+ return (
31+ < span > { queue . length } </ span >
32+ ) ;
33+ }
34+ }
35+
36+ class Foo extends Component {
37+ static propTypes = {
38+ queue : ObservablePropTypes . observableObject
39+ }
40+ render ( ) {
41+ const cache = this . props . cache
42+ return (
43+ < div >
44+ < button onClick = { this . props . refresh } > 点击</ button >
45+ < Bar queue = { cache . queue } > </ Bar >
46+ </ div >
47+ ) ;
48+ }
49+ }
50+
51+ ReactDOM . render ( < Foo cache = { store . cache } refresh = { store . refresh } /> , document . querySelector ( '#root' ) )
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " mobx1" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " " ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1" ,
8+ "start" : " webpack -w"
9+ },
10+ "keywords" : [],
11+ "author" : " " ,
12+ "license" : " ISC" ,
13+ "devDependencies" : {
14+ "babel-core" : " ^6.26.3" ,
15+ "babel-loader" : " ^7.1.5" ,
16+ "babel-plugin-transform-class-properties" : " ^6.24.1" ,
17+ "babel-plugin-transform-decorators-legacy" : " ^1.3.5" ,
18+ "babel-preset-env" : " ^1.7.0" ,
19+ "babel-preset-react" : " ^6.24.1" ,
20+ "webpack" : " ^4.41.1" ,
21+ "webpack-cli" : " ^3.3.9"
22+ },
23+ "dependencies" : {
24+ "mobx" : " ^5.14.0" ,
25+ "mobx-react" : " ^6.1.4" ,
26+ "prop-types" : " ^15.7.2" ,
27+ "react" : " ^16.10.2" ,
28+ "react-dom" : " ^16.10.2"
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ const path = require ( 'path' )
2+
3+ const config = {
4+ mode : 'development' ,
5+ entry : path . resolve ( __dirname , 'src/index.jsx' ) ,
6+ output : {
7+ path : path . resolve ( __dirname , 'dist' ) ,
8+ filename : 'main.js'
9+ } ,
10+ module : {
11+ rules : [ {
12+ test : / \. j s x ? $ / ,
13+ exclude : / n o d e _ m o d u l e s / ,
14+ use : {
15+ loader : 'babel-loader' ,
16+ options : {
17+ presets : [ 'env' , 'react' ] ,
18+ plugins : [ 'transform-decorators-legacy' , 'transform-class-properties' ]
19+ }
20+ }
21+ } ]
22+ } ,
23+ devtool : 'inline-source-map'
24+ }
25+
26+ module . exports = config
You can’t perform that action at this time.
0 commit comments