Search code examples
reduxredux-middleware

redux: set initial state along with compose middleware


The normal way to set initial state (that overrides a reducer's own default initial state) while creating a store is to provide a second argument to createStore (how to set initial state in redux). But I've unfortunately cobbled together a tangle of stuff (pardon the mixed metaphor) that I don't really understand and I don't know where to put my desired initial state:

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
  rootReducer,
  composeEnhancers(
    applyMiddleware(...middleware)
  )
);

I hope no one asks to see my combineReducers and middleware construction code 'cause that's even more embarrassing.


Solution

  • Still use second argument:

    const store = createStore(
      rootReducer,
      { your: { initial: { state: {} } } },
      composeEnhancers(
        applyMiddleware(...middleware)
      )
    );