grofers / redux-cookies-middleware

redux-cookies-middleware is a Redux middleware which syncs a subset of your Redux store state with cookies.
MIT License
81 stars 16 forks source link

getStateFromCookies returns a funstion not a state #43

Closed Antibioticvz closed 6 years ago

Antibioticvz commented 7 years ago

In this implementation after getStateFromCookies we have

ƒ (store) { return function (next) { return function (action) { var prevState = store.getState(); var result = next(action); var next…

I don't understand is it bug or my problem

import { createStore, applyMiddleware, compose } from 'redux';
import reducers from './reducers';
import thunk from 'redux-thunk';

import reduxCookiesMiddleware from 'redux-cookies-middleware';
import getStateFromCookies from 'redux-cookies-middleware';

export default function configureStore(initialState={}) {

    // state to persist in cookies
    const paths = {
        'login.token': { name: 'this_token' },
    };

  // Create the store with two middlewares
  const middlewares = [
    thunk,
    reduxCookiesMiddleware(paths),
  ];

  let enhancers;
  if (process.env.NODE_ENV === 'production') enhancers = [applyMiddleware(...middlewares)];
  else {
    enhancers = [
      applyMiddleware(...middlewares),
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
    ];
  }

  // read stored data in cookies and merge it with the initial state
    initialState = getStateFromCookies(initialState, paths);

  const store = createStore(
    reducers
    , initialState
    , compose(...enhancers),
  );

  // Extensions
  store.asyncReducers = {}; // Async reducer registry

  return store;
}
nimishsinghal commented 7 years ago

@Antibioticvz I think you should use import getStateFromCookies from 'redux-cookies-middleware/getStateFromCookies'; or import { getStateFromCookies } from 'redux-cookies-middleware'; instead of import getStateFromCookies from 'redux-cookies-middleware';

Antibioticvz commented 7 years ago

@nimishsinghal does not work. the import getStateFromCookies from 'redux-cookies-middleware/getStateFromCookies'; does not work at all. import { getStateFromCookies } from 'redux-cookies-middleware'; the same problem. import getStateFromCookies from 'redux-cookies-middleware/src/getStateFromCookies'; and import getStateFromCookies from 'redux-cookies-middleware/lib/getStateFromCookies'; the same problem

vaidik commented 7 years ago

@Antibioticvz so in the script that you shared previously, you have two imports in the beginning:

import reduxCookiesMiddleware from 'redux-cookies-middleware';
import getStateFromCookies from 'redux-cookies-middleware';

Both the statements are essentially the same. And what you will get is the middleware as a result in both reduxCookiesMiddleware and getStateFromCookies. Although what you intend to do in the second statement is different from the result.

The first thing you need to do is change the above snippet to:

import reduxCookiesMiddleware from 'redux-cookies-middleware';
import { getStateFromCookies } from 'redux-cookies-middleware';

This should fix the first problem that we see here. Can you make this change and try to run your script? If you don't get your expected result, please put a like this console.log(getStateFromCookies) after your import statements and share what you are getting here?

Thanks

anubhav7495 commented 6 years ago

Closing due to no activity for a long time.