Kaynağa Gözat

mobx持久化

胡家华 5 yıl önce
ebeveyn
işleme
b249e5416c

+ 17 - 0
config-overrides.js

@@ -0,0 +1,17 @@
1
+// import { override, fixBabelImports }  from 'customize-cra'
2
+const { override, fixBabelImports, addLessLoader,disableEsLint, addDecoratorsLegacy,useBabelRc } = require('customize-cra');
3
+
4
+module.exports = override(
5
+    addDecoratorsLegacy(),
6
+    disableEsLint(),
7
+    useBabelRc(),
8
+    fixBabelImports('import', {
9
+        libraryName: 'antd',
10
+        libraryDirectory: 'es',
11
+        style: true
12
+    }),
13
+    addLessLoader({
14
+        javascriptEnabled: true,
15
+        modifyVars: { '@primary-color': '#1DA57A' },
16
+    })
17
+);

Dosya farkı çok büyük olduğundan ihmal edildi
+ 12758 - 0
package-lock.json


+ 21 - 4
package.json

@@ -3,14 +3,31 @@
3 3
   "version": "0.1.0",
4 4
   "private": true,
5 5
   "dependencies": {
6
+    "@babel/plugin-proposal-class-properties": "^7.5.5",
7
+    "@babel/plugin-proposal-decorators": "^7.4.4",
8
+    "antd": "^3.22.1",
9
+    "babel-plugin-import": "^1.12.0",
10
+    "babel-plugin-transform-decorators-legacy": "^1.3.5",
11
+    "babel-preset-mobx": "^2.0.0",
12
+    "customize-cra": "^0.5.0",
13
+    "less": "^3.10.3",
14
+    "less-loader": "^5.0.0",
15
+    "mobx": "^5.11.0",
16
+    "mobx-persist": "^0.4.1",
17
+    "mobx-react": "^6.1.3",
6 18
     "react": "^16.8.6",
19
+    "react-app-rewire-mobx": "^1.0.9",
20
+    "react-app-rewired": "^2.1.3",
7 21
     "react-dom": "^16.8.6",
8
-    "react-scripts": "3.0.1"
22
+    "react-loadable": "^5.5.0",
23
+    "react-router-dom": "^5.0.1",
24
+    "react-scripts": "3.0.1",
25
+    "serializr": "^1.5.1"
9 26
   },
10 27
   "scripts": {
11
-    "start": "react-scripts start",
12
-    "build": "react-scripts build",
13
-    "test": "react-scripts test",
28
+    "start": "react-app-rewired start",
29
+    "build": "react-app-rewired build",
30
+    "test": "react-app-rewired test",
14 31
     "eject": "react-scripts eject"
15 32
   },
16 33
   "eslintConfig": {

+ 6 - 33
src/App.css

@@ -1,33 +1,6 @@
1
-.App {
2
-  text-align: center;
3
-}
4
-
5
-.App-logo {
6
-  animation: App-logo-spin infinite 20s linear;
7
-  height: 40vmin;
8
-  pointer-events: none;
9
-}
10
-
11
-.App-header {
12
-  background-color: #282c34;
13
-  min-height: 100vh;
14
-  display: flex;
15
-  flex-direction: column;
16
-  align-items: center;
17
-  justify-content: center;
18
-  font-size: calc(10px + 2vmin);
19
-  color: white;
20
-}
21
-
22
-.App-link {
23
-  color: #61dafb;
24
-}
25
-
26
-@keyframes App-logo-spin {
27
-  from {
28
-    transform: rotate(0deg);
29
-  }
30
-  to {
31
-    transform: rotate(360deg);
32
-  }
33
-}
1
+@import '~antd/dist/antd.css';
2
+.flex_center{
3
+    justify-content: center;
4
+    align-items: center;
5
+    flex-direction: row;
6
+}

+ 13 - 25
src/App.js

@@ -1,26 +1,14 @@
1
-import React from 'react';
2
-import logo from './logo.svg';
3
-import './App.css';
4
-
5
-function App() {
6
-  return (
7
-    <div className="App">
8
-      <header className="App-header">
9
-        <img src={logo} className="App-logo" alt="logo" />
10
-        <p>
11
-          Edit <code>src/App.js</code> and save to reload.
12
-        </p>
13
-        <a
14
-          className="App-link"
15
-          href="https://reactjs.org"
16
-          target="_blank"
17
-          rel="noopener noreferrer"
18
-        >
19
-          Learn React
20
-        </a>
21
-      </header>
22
-    </div>
23
-  );
24
-}
25 1
 
26
-export default App;
2
+import React from 'react';
3
+import RootRouter from './route'
4
+import store from './store'
5
+import { Provider } from 'mobx-react'
6
+import './App.css'
7
+export default function App (props) {
8
+    return (
9
+        <Provider { ...store }>
10
+            <RootRouter/>
11
+        </Provider>
12
+    );
13
+  
14
+}

+ 4 - 0
src/components/Counter/index.css

@@ -0,0 +1,4 @@
1
+.counterSpan{
2
+    padding: 15px;
3
+    display: inline-block;
4
+}

+ 30 - 0
src/components/Counter/index.js

@@ -0,0 +1,30 @@
1
+import React, { Component } from 'react';
2
+import { Button } from 'antd';
3
+import { inject, observer } from 'mobx-react'
4
+import './index.css'
5
+
6
+@inject('counterStore')
7
+@observer
8
+export default class MobxPersist extends Component {
9
+  componentDidMount () {
10
+    console.log('MobxPersist', this.props)
11
+  }
12
+  increaseNum = () => {
13
+    this.props.counterStore.increaseNum();
14
+  }
15
+  decrease = () => {
16
+    this.props.counterStore.decrease();
17
+  }
18
+  render () {
19
+    const { counterStore } = this.props
20
+    console.log('this.props.counterStore.getPow', this.props.counterStore.getPow)
21
+    return (
22
+      <div className='flex_center countValueWrap'>
23
+        <Button type="primary" onClick={this.increaseNum}>加</Button>
24
+        <div className='counterSpan'>{counterStore.count}</div>
25
+        <Button type="primary" onClick={this.decrease}>减</Button>
26
+        <div>Math.pow = {this.props.counterStore.getPow}</div>
27
+      </div>
28
+    );
29
+  }
30
+}

+ 24 - 0
src/components/Counter/store.js

@@ -0,0 +1,24 @@
1
+import { action, computed, observable } from 'mobx'
2
+import { persist } from 'mobx-persist'
3
+import { serializable, list, object } from 'serializr'
4
+
5
+class CounterStore {
6
+    @persist @observable count = 0;
7
+    
8
+    @computed
9
+    get getPow() {
10
+        return this.count * this.count
11
+    } 
12
+
13
+    @action
14
+    increaseNum () {
15
+        this.count = ++this.count
16
+    }
17
+
18
+    @action
19
+    decrease () {
20
+        this.count = --this.count
21
+    }
22
+}
23
+const counterStore = new CounterStore
24
+export default counterStore

+ 3 - 0
src/route/history.js

@@ -0,0 +1,3 @@
1
+import createHistory from 'history/createHashHistory'
2
+
3
+export default createHistory()

+ 24 - 0
src/route/index.js

@@ -0,0 +1,24 @@
1
+
2
+
3
+import React from 'react';
4
+import { Switch, Route, Router, Redirect  } from 'react-router-dom'
5
+import routeTree from './router'
6
+import history from './history'
7
+export default function RootRouter (props) {
8
+    return (
9
+        <Router history={history}>
10
+            <Switch>
11
+                { 
12
+                    routeTree.map(({ path, name, component})=> {
13
+                        return (
14
+                            <Route key={name} exact path={path} component={component} />
15
+                        )
16
+                    })
17
+                }
18
+                <Route path="/404" component={() => { return <h1>404</h1>}}/>
19
+                <Redirect to='/404' />
20
+            </Switch>
21
+        </Router>
22
+    );
23
+  
24
+}

+ 13 - 0
src/route/router.js

@@ -0,0 +1,13 @@
1
+
2
+import { loadable } from '../utils'
3
+export default [
4
+    {
5
+        path: '/',
6
+        name: 'Home',
7
+        component:  loadable(import('../views/Home'))
8
+    }, {
9
+        path: '/mobxPersiste',
10
+        name: 'mobx持久化测试',
11
+        component:  loadable(import('../views/MobxPersiste'))
12
+    }
13
+]

+ 14 - 0
src/store/index.js

@@ -0,0 +1,14 @@
1
+import nav from './nav'
2
+import counterStore from '../components/Counter/store'
3
+import { create } from 'mobx-persist'
4
+const hydrate = create({
5
+    storage: localStorage,
6
+    jsonify: true
7
+})
8
+hydrate('_Shop', counterStore).then(() => console.log('someStore has been hydrated'))
9
+hydrate('nav', nav).then(() => console.log('someStore has been hydrated'))
10
+
11
+export default {
12
+    counterStore,
13
+    nav,
14
+}

+ 13 - 0
src/store/nav.js

@@ -0,0 +1,13 @@
1
+import { action, computed, observable } from 'mobx'
2
+import history from '../route/history'
3
+const { push, go, goBack, goForward, replace } = history
4
+class NavStore {
5
+
6
+    @action
7
+    push ({ pathname = '/', query = {} }) {
8
+        console.log('pathname',pathname)
9
+        push({ pathname, query })
10
+    }
11
+}
12
+const navStore = new NavStore
13
+export default navStore

+ 6 - 0
src/utils/index.js

@@ -0,0 +1,6 @@
1
+
2
+export { default as loadable } from './loadable'
3
+
4
+export const modalConsole = (modelName, error) => {
5
+    console.log(`%cThe error in ${modelName}.js`,'color:red', error)          
6
+}

+ 13 - 0
src/utils/loadable.js

@@ -0,0 +1,13 @@
1
+import React from 'react'
2
+import Loadable from 'react-loadable' 
3
+const loadingComponent =()=>{
4
+    return <div>你瞅啥...</div>
5
+}
6
+export default function loadable(loader, loading = loadingComponent) {
7
+    if (!loader) throw new Error('??excuse me??!');
8
+    return Loadable({
9
+        loader: () => loader,
10
+        loading,
11
+    })   
12
+}
13
+

+ 13 - 0
src/views/Home/index.css

@@ -0,0 +1,13 @@
1
+.navWrap{
2
+    justify-content: flex-start;
3
+    align-items: center;
4
+    flex-wrap: wrap;
5
+    padding: 10px;
6
+}
7
+.testItemWrap{
8
+    background: purple;
9
+    color: #fff;
10
+    display: inline-block;
11
+    padding: 4px 8px 4px 8px;
12
+    border-radius: 6px;
13
+}

+ 31 - 0
src/views/Home/index.js

@@ -0,0 +1,31 @@
1
+import React, { Component } from 'react';
2
+import { Button } from 'antd'
3
+import { inject, observer } from 'mobx-react'
4
+import './index.css'
5
+
6
+const nav = [{
7
+  name: 'mobx持久化测试',
8
+  route: '/mobxPersiste'
9
+}]
10
+@inject('nav')
11
+@observer
12
+export default class Home extends Component {
13
+  handleGotoPage =(route) => {
14
+    this.props.nav.push({ pathname: route })
15
+  }
16
+  render () {
17
+    return (
18
+      <div className='navWrap'>
19
+        {
20
+          nav.map((item, index) => {
21
+            return (
22
+              <Button type='primary' key={index} onClick={() => { this.handleGotoPage(item.route) }}>
23
+                {item.name}
24
+              </Button>
25
+            )
26
+          })
27
+        }
28
+      </div>
29
+    )
30
+  }
31
+}

+ 7 - 0
src/views/MobxPersiste/index.js

@@ -0,0 +1,7 @@
1
+import React, { Component } from 'react';
2
+import Counter from '../../components/Counter'
3
+export default class MobxPersist extends Component {
4
+    render () {
5
+        return <Counter/>
6
+    }
7
+}

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1345 - 28
yarn.lock