Redux 很简单,简单到什么程度呢?

下面短短几行代码,就是 Redux 的核心:)

export function createStore(reducer) {
  let state = null;
  const listeners = [];
  const subscribe = (listener) => listeners.push(listener);
  const getState = () => state;
  const dispatch = (action) => {
    state = reducer(state, action);
    listeners.forEach( (listener) => {
      listener()
    })
  }
  dispatch({});
  return { getState, dispatch, subscribe };
}