DEV Community

Discussion on: Redux reimplemented in 12 lines of code

Collapse
 
vonheikemen profile image
Heiker

And with a few more you can have redux-thunk.

const createStore = reducer => {
  let onChange;
  let state = reducer(undefined, {});
+ let store = {
- return {
    getState: () => state,
    subscribe: fn => (onChange = fn),
    dispatch: action => {
+     if(typeof action === 'function') {
+       return action(store.dispatch, store.getState);
+     }
+
      state = reducer(state, action);
      onChange();
    }
  };

+ return store;
};

A bit intrusive but it does the job.