Redux底层源码

Redux底层源码

  • 底层就是发布与订阅模式
const creareStore = (reducer)=> {
  let list = [];
  let state = reducer()
  const subscribe = (callback)=> {
    list.push(callback)
  }
  const dispatch = (action)=> {
    state = reducer(state, action)
    list.forEach((itemCallback)=> {
      itemCallback()
    })
  }
  const getState = ()=> {
    return state;
  }
  return {
    subscribe,
    dispatch,
    getState
  }
}
posted @ 2022-12-08 01:40  HuangBingQuan  阅读(18)  评论(0编辑  收藏  举报