zoukankan      html  css  js  c++  java
  • redux

    1. 前言

    redux 基本思想是保证数据的单向流动,同时便于控制、使用、测试。

    2. 主干逻辑介绍(createStore)

    demo

    // 首先定义一个改变数据的plain函数,成为reducer
    function count (state, action) {
        var defaultState = {
            year: 2015,
          };
        state = state || defaultState;
        switch (action.type) {
            case 'add':
                return {
                    year: state.year + 1
                };
            case 'sub':
                return {
                    year: state.year - 1
                }
            default :
                return state;
        }
    }
    
    // store的创建
    var createStore = require('redux').createStore;
    var store = createStore(count);
    
    // store里面的数据发生改变时,触发的回调函数
    store.subscribe(function () {
          console.log('the year is: ', store.getState().year);
    });
    
    // action: 触发state改变的唯一方法(按照redux的设计思路)
    var action1 = { type: 'add' };
    var action2 = { type: 'add' };
    var action3 = { type: 'sub' };
    
    // 改变store里面的方法
    store.dispatch(action1); // 'the year is: 2016
    store.dispatch(action2); // 'the year is: 2017
    store.dispatch(action3); // 'the year is: 2016
    

    .

  • 相关阅读:
    【lc-database】595. 大的国家
    Visual Studio 2010软件安装教程
    Win10系统下安装VC6.0教程
    HTTP协议
    正则表达式
    类装饰器
    装饰器工厂函数
    装饰器函数
    闭包
    web服务器
  • 原文地址:https://www.cnblogs.com/crazycode2/p/7215463.html
Copyright © 2011-2022 走看看