JS Built-ins
redux-legacycreateStoreadvanced

Store Enhancer Pattern

πŸ“– Usage Example
const store = createStore(reducer, applyMiddleware(logger, thunk))

store.dispatch({ type: 'INCREMENT' })
// β†’ logger logs, thunk checks type, state updates

Store enhancers let you wrap createStore to intercept dispatch, enabling middleware, devtools, and more.

Store Enhancer Pattern

A store enhancer wraps createStore to add capabilities (like middleware). The signature is:

const enhancer = createStore => reducer => store

applyMiddleware is Redux's built-in enhancer factory. When createStore receives an enhancer as its second argument it delegates to it:

function createStore(reducer, enhancer) {
  if (typeof enhancer === 'function') return enhancer(createStore)(reducer)
  // ...normal store creation
}

Challenge: Implement both createStore(reducer, enhancer) and applyMiddleware(middleware).

πŸ’‘ Hints (2)
  • β€’ `if (typeof enhancer === 'function') return enhancer(createStore)(reducer)` is the only change needed in createStore.
  • β€’ `applyMiddleware` must return a function that takes `createStore`, then `reducer` β€” matching the enhancer signature.