rt2zz / redux-action-buffer

Buffer Redux Actions
MIT License
79 stars 6 forks source link

Trying to use redux-action-buffer with redux-persist #9

Closed karelbilek closed 7 years ago

karelbilek commented 7 years ago

I am trying to use redux-persist with redux-action-buffer, but it doesn't seem to be working as described in the documentation.

My simple example is as follows:

import {applyMiddleware, createStore, compose} from 'redux';
import {persistStore, autoRehydrate} from 'redux-persist';
import createActionBuffer from 'redux-action-buffer';
import {REHYDRATE} from 'redux-persist/constants';

function reducer(state = {foo: ["bar"]}, action) {
  if (action.type === 'ADD_FOO') {
    return {
      foo: [...state.foo, action.item]
    }
  }
  return state;
}

const enhancer = compose(
  autoRehydrate(),
  applyMiddleware(
    createActionBuffer(REHYDRATE)
  )
);

const store = createStore(
  reducer,
  undefined,
  enhancer
);

persistStore(store);

store.dispatch({
  type: 'ADD_FOO',
  item: 'baz'
});

store.dispatch({
  type: 'ADD_FOO',
  item: 'hocus pocus'
});

setTimeout(() => {
  // the state is correct...
  console.log(store.getState());
  // but nothing in the localStorage!
  console.log(localStorage);
}, 1000);

According to redux-persist documentation and redux-action-buffer documentation, this should work. Yet it doesn't and nothing is saved to localstorage.

If I change the store.dispatch code to run inside the callback, as in

persistStore(store, undefinded, () => {
    store.dispatch(....

it seems to be working; but in that case, the redux-action-buffer is not doing what it should and it doesn't actually buffer anything.

I am beginning with Redux so I might be missing something. :)

rt2zz commented 7 years ago

that is curious, code looks correct. The only thing I can think of that is "atypical" is not using combineReducers. Still I would expect this to work.

I will try to give your code a run at some point in the future. In the meantime I might suggest adding combineReducers and see if that helps, or debugging persistStore to see what happens when it receives the state subscription.

karelbilek commented 7 years ago

I copy-pasted the code from online examples. :)

I will try the combineReducers then

karelbilek commented 7 years ago

combineReducers doesn't help, it still doesn't work.

I will look at persistStore