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

TypeError: next is not a function #80

Open JimLynchCodes opened 6 years ago

JimLynchCodes commented 6 years ago

Hi, I'm trying to add persistState , but it's giving me this error:

TypeError: next is not a function
(anonymous function)
C:/Git-Projects/reacttr/node_modules/redux-localstorage/lib/persistState.js:70
  67 |   console.warn('Failed to retrieve initialize state from localStorage:', e);
  68 | }
  69 | 
> 70 | var store = next(reducer, finalInitialState, enhancer);
  71 | var slicerFn = slicer(paths);
  72 | 
  73 | store.subscribe(function () {
View compiled

Here's my store.js file:

import {applyMiddleware, createStore} from 'redux';
import {composeWithDevTools} from 'redux-devtools-extension';
import ReduxLogger from 'redux-logger';
import thunk from 'redux-thunk';
import promise from 'redux-promise-middleware';
import reducer from '../reducers';
import persistState from 'redux-localstorage';

const middleware = applyMiddleware(persistState(), promise(), thunk, ReduxLogger);

const initialState = {
};

function getHeaderTooltip(headerName) {
  return "sum(" + headerName.toString() + ")";
}

export default createStore(reducer, initialState, composeWithDevTools(middleware));
marcelorl commented 6 years ago

I've figure out how to use it. This error happens, when we try to add redux-localstorage to applyMiddleware function, like this:

const middlewares = applyMiddleware(
  thunkMiddleware,
  persistState(['cart'])
);

createStore accepts as a second parameter middlewares or an enhancer.

Then, your code would be something like this:

import { combineReducers, createStore, applyMiddleware, compose } from 'redux';

const reducers = combineReducers({
  cart: cartReducer
});

const middlewares = applyMiddleware(
  thunkMiddleware
);

const configureStore =
  createStore(
    reducers,
    compose(
      middlewares,
      persistState(['cart'])
    )
  );

Pretty simple. I hope I still may help someone.

This issue can be closed.