Revisolution / typed-redux-kit

Scaffoldings for Redux with Typescript
19 stars 5 forks source link

Add support for initial state when using a mapped reducer with modules #2

Closed thomhos closed 7 years ago

thomhos commented 7 years ago

If we want to use a mapped reducer with module configuration we need to pass a default state when initialising the reducer.

Originally we would do this:

const reducer = (state = defaultState, action) ...

We could provide it as an argument when making the MappedReducer and set 'this.initialState' in the constructor of the MappedReducer. Then we can add the same logic as above to the .reduce method of the class.

public reduce = (state: STATE = this.initialState, action: ACTION): STATE => {
    if (!this.reducerMap.has(action.type)) return state
    return this.reducerMap.get(action.type)(state, action)
}

How do you guys think about that?

Rokt33r commented 7 years ago

Definitely. This should make it work.

interface Options<S = any> {
  initialState: S
}
class MappedReducer<S> {
  constructor(opts: Options<S>) {
    this.initialState = opts.initialState
  }
}

I'll fix it today!

thomhos commented 7 years ago

Need to make sure the initial state is optional though in case we don't want to use as module. Cheers!

thomhos commented 7 years ago

Fixed in pr #3