zoukankan      html  css  js  c++  java
  • dva-counter

    dva实例Counter

    import dva, { connect } from 'dva';
    import { Router, Route } from 'dva/router';
    import styles from './index.less';
    
    const app = dva();
    
    function XX() {
      this.events = {};
    }
    
    XX.prototype.addListener = function(event, listener) {
      if(!this.events.hasOwnProperty(event)) {
        this.events[event] = listener;
      }
    }
    
    XX.prototype.trigger = function(event) {
      if(this.events.hasOwnProperty(event)) {
        (this.events[event])()
      }
    }
    
    const xx = new XX();
    
    app.model({
      namespace: 'count',
      state: {
        record: 0,
        current: 0,
      },
      reducers: {
        add(state) {
          const newCurrent = state.current + 1;
          return {
            ...state,
            record: newCurrent > state.record ? newCurrent : state.record,
            current: newCurrent
          }
        },
        minus(state) {
          return {
            ...state, 
            current: state.current - 1
          };
        },
      },
      subscriptions: {
        clickWatcher({dispatch}) {
          xx.addListener('click', () => {dispatch({type: 'minus'})})
        }
      },
      effects: {
        *add(action, {call, put}) {
          yield call(delay, 1000);
          yield put({type: 'minus'});
        }
      }
    });
    
    const SubButton = () => {
      return (
        <button onClick={() => xx.trigger('click')}>subButton</button>
      )
    }
    
    const CountApp = ({count, dispatch}) => {
      return (
        <div className={styles.normal}>
          <div className={styles.record}>Highest Record: {count.record}</div>
          <div className={styles.current}>{count.current}</div>
          <div className={styles.button}>
            <button onClick={() => { dispatch({type: 'count/add'}); }}>+</button>
          </div>
          <SubButton />
        </div>
      )
    }
    
    function mapStateToProps(state) {
      return {
        count: state.count
      };
    }
    
    const HomePage = connect(mapStateToProps)(CountApp);
    
    app.router(({history}) => 
      <Router history={history}>
        <Route path="/" component={HomePage} />
      </Router>
    )
    
    app.start('#root');
    
    // -------------
    // Helpers
    
    function delay(timeout) {
      return new Promise(resolve => {
        setTimeout(resolve, timeout)
      })
    }
    

    .normal {
       200px;
      margin: 100px auto;
      padding: 20px;
      border: 1px solid #ccc;
      box-shadow: 0 0 20px #ccc;
    }
    
    .record {
      border-bottom: 1px solid #ccc;
      padding-bottom: 8px;
      color: #ccc;
    }
    
    .current {
      text-align: center;
      font-size: 40px;
      padding: 40px 0;
    }
    
    .button {
      text-align: center;
      button {
         100px;
        height: 40px;
        background: #aaa;
        color: #fff;
      }
    }
    
  • 相关阅读:
    eclipse中将web项目部署到tomcat
    mysql + Fluently NHibernate + WebAPI + Autofac
    无状态
    mysql备份及还原
    【jmeter】jmeter 常用组件 介绍
    【测试小方法】通过谷歌开发者工具手动修改位置信息
    【jmeter】jmeter 设置线程组 各个字段的概念
    【jmeter】jmete 安装介绍
    【腾讯位置服务web service】腾讯位置服务web service api
    【python】python 的 lambda 函数
  • 原文地址:https://www.cnblogs.com/shineyao/p/7912851.html
Copyright © 2011-2022 走看看