react之自定义迷你redux的实现

export function createStore(reducer){
  let currentState={}
  let currentListeners=[]
  function getState(){
    return currentState
  }
  function subscribe(listener){
    currentListeners.push(listener)
  }
  function dispatch(action){
    debugger
    currentState=reducer(currentState,action)
    currentListeners.forEach(v=>v())
    return action
  }
  dispatch({type:'@7832@@%%%%'}) //初始化状态
  return {getState,subscribe,dispatch}
}

原理:基于发布与订阅的模式

猜你喜欢

转载自www.cnblogs.com/raind/p/9690076.html