elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

How to use custom middleware #42

Closed rbrahul closed 8 years ago

rbrahul commented 8 years ago

I am trying to use a custom middleware in store in following process. It's not working. Can you please suggest how to add custom middleware? Thanks in advance

const createPersistentStore = compose(
    persistState()
)(createStore);

 const store = createPersistentStore(
     connectApp,
     initialState,
     compose(applyMiddleware(...middleware), extension)
 );
export default store;
krksgbr commented 8 years ago

createPersistentStore should only receive your reducer and initialState. you probably need to pass your middleware to the compose with which you create createPersistentStorage here's how i made it work with redux dev-tools:

const createPersistentStore = compose(
    persistState(),
    window.devToolsExtension ? window.devToolsExtension() : f => f
)(createStore);

const store = createPersistentStore(reducer, initialState);
elgerlambert commented 8 years ago

Hi @rbrahul,

Redux-localstorage was missing support for createStore's third "enhancer" argument. This has been fixed in 1.0.0-rc5; the code you had should work now.

I would however recommend sticking to one convention when it comes to enhancing your store (either through functional composition or by using the enhancer argument), e.g.:

const store = createPersistentStore(
     connectApp,
     initialState,
     compose(
         applyMiddleware(...middleware),
         extension,
         persistState()
    )
 );

export default store;