neurosnap / robodux

caching in redux made simple
MIT License
101 stars 7 forks source link

Add `initialState` property to reducer? #2

Closed venkatd closed 6 years ago

venkatd commented 6 years ago

Great work on this lib! Just swapped over and it removed a lot of boilerplate.

Currently I have to write: const store = createStore<StoreState, any, any, any>(RootReducer.reducer, {commands: [], events: []}, enhancer)

I would prefer to write: const store = createStore<StoreState, any, any, any>(RootReducer.reducer, RootReducer.initialState, enhancer);

I have already defined the initial state so think I should be able to access the initialState directly. Thoughts?

neurosnap commented 6 years ago

Thanks for the compliment!

So If I understand you correctly, you would like me to return initialState from the robodux function?

venkatd commented 6 years ago

Yeah exactly. I suppose you'd have to add an entry here? https://github.com/neurosnap/robodux/blob/master/src/slice.ts#L53

Also, I'm guessing you would somehow need to combine the initial states to support this with combinedReducers

neurosnap commented 6 years ago

Yep! I'm totally okay with this change. You are more than welcome to submit a PR and I'll merge and deploy to npm

venkatd commented 6 years ago

@neurosnap on second thought I wonder if it's better to have this belong to the reducer instead of the slice.

Meaning: createStore<StoreState, any, any, any>(RootSlice.reducer, RootSlice.reducer.initialState, enhancer);

I conflated slices and reducers. What do you think?

venkatd commented 6 years ago

Another update haha. I don't think this is the way to go after having read this: https://redux.js.org/recipes/structuringreducers/initializingstate#recap

Seems like I should be able to write createStore(RootSlice.reducer, {}, enhancer) but not sure if this is a limitation of the redux typings or robodux. Will write back once I've dug into this more.

neurosnap commented 6 years ago

Does createStore(RootSlice.reducer, undefined, enhancer) work?

venkatd commented 6 years ago

Interestingly it compiles and runs but the vscode editor complains with the following: Argument of type 'undefined' is not assignable to parameter of type 'DeepPartial<any>'.

venkatd commented 6 years ago

I don't think there is anything to be done with robodux since the issue (if any) is with the createStore type signature. Potentially I could issue a PR for the readme explaining different approaches to create the store.