zoukankan      html  css  js  c++  java
  • 【react】---手动封装一个简易版的redux---【巷子】

    export let createStore = (reducer)=>{
        //定义默认的state
        let state = {};
    
        //定义默认的action
        let actionTypes = "@@redux/INIT"+Math.random();
        let initAction = {type:actionTypes}
    
        //将所以需要监听的函数放在这个里面
        let listeners = []
    
        //定义getState函数
        let getState = ()=>state;
    
        //定义事件订阅函数
        let subscribe = (cb)=>{
            listeners.push(cb);
        }
    
        //定义事件派发函数 用来调用action
        let dispatch = (action=initAction)=>{
           
            //调用reducer获取新的state
            state = reducer(state,action);
    
            //遍历所以需要监听的函数
            listeners.map((cb)=>{
                cb();
            })
            
    
        }
        dispatch();
    
        return {
            getState,
            dispatch,
            subscribe
        }
    }

    const combineReducers = (reducers)=>{
     
      let newState = {};
      return function(state,action){
     
        for(var key in reducers){
          newState[key] = reducers[key](state[key],action)
        }
     
        return newState;
      }
    }
     
  • 相关阅读:
    idea自带的maven
    面试题汇总
    mybatis参数处理
    tips
    mybatis-config.xml
    helloWorld程序
    idea遇到的问题汇总
    PL/SQL批量执行SQL脚本文件
    Iframe跳转本地项目
    angular video播放问题
  • 原文地址:https://www.cnblogs.com/nanianqiming/p/10480758.html
Copyright © 2011-2022 走看看