omnidan / redux-undo

:recycle: higher order reducer to add undo/redo functionality to redux state containers
MIT License
2.91k stars 188 forks source link

undoable returns initial state for dynamically added reducer. #277

Open SunhoY opened 4 years ago

SunhoY commented 4 years ago

Note: for support questions about the usage of the library, please ask questions on the gitter chat. The issue tracker is reserved for feature requests, bug reports and similar issues. Visit the gitter chat here.

I'm submitting a ...

What is the current behavior/state of the project?

I'm using Reducer Manager from here When I add new reducer and combined it, newly added state would be undefined. for example.

interface ProjectState {
  name: string;
}

const initialState: {
   initialProject: projectState, // this is StateWithHistory,
}

...
// from some place
reducerManager.add('newProject', undoable(...));

Since no actions are dispatched, store state would be same with initialState.

as soon as I dispatch some action, state would be

{
  initialProject: projectState, // existing state
  newProject: newProjectState, // this will be initial value of StateWithHistory
}

luckily, when I started app, redux seems dispatch @@INIT action, so all our states are well formed.

but if I try to dispatch action which interacts with new reducer(which is undoable) right after adding new reducer. because of this statement it returns initialState of StateWithHistory instead of calling reducer. I'm not sure it's a bug or intended behavior.

{
  initialProject: ...existingState,
  newProject: {
    future: [],
    past: [],
    present: ...initialProjectState, // defined initial state in reducer,
    ...,
  }
}

What is the desired behavior?

reducer function should be called with initialState of StateWithHistory and run reducer logic with given action.

If this is a feature request, what is the use case for changing the behavior?

dynamic reducers described above.

use case, Tabs, adding tabs to tab bar, and each tab has it's own history.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via a gist or similar.

Please tell us about your environment:

Other information

(e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc)