dphilipson / typescript-fsa-reducers

Fluent syntax for defining typesafe reducers on top of typescript-fsa.
MIT License
220 stars 16 forks source link

Add ability to pass default case handler #27

Closed monsterkrampe closed 6 years ago

monsterkrampe commented 6 years ago

I came across the following usecase (I think this also relates to #23):

First I have a reducers with some state like

{
    ...someState,
    config,
}

This reducers needs to read from config. Also there is some logic on how config should behave which basically has nothing to do with ...someState, hence config gets its own reducer.

So normally I would place config somewhere outside, but then I cannot access config in my first reducer (I don't want to trigger new actions using thunk or saga for this).

Without typescript-fsa-reducers I would just call the reducer for config on every action in the first reducer. (Because of the nesting I cannot use combineReducer so I basically implement a simlar behaviour myself.)

switch (action.type) {
    case 'A': return { ...state, someProp: 2, config: configReducer(state.config, action) };
    ...
    default: return { ...state, config: configReducer(state.config, action) };
}

I don't know if this is some kind of bad practice, but it seemed clean enough to me.

Unfortunatly I'm not able include a default case when I use typescript-fsa-reducers.

Because I needed a solution quite fast, I ended up forking this repo and added the possibility to add a default handler and included my fork in the project.

Now I will submit a PR that adds this functionality.

If you have any suggestion how I could do this in a similar and clean or cleaner manner without having to alter the default case, I would appreciate this, then the PR can be deleted.