Reusing actions across multiple reducers in redux
<p>So I'm working on a large react/redux app and have been very happy with how simple it is manage state in redux. We are using a  "ducks" style approach to reducers/action/action Creators to keep things relatively domain based. In addition, we try to keep ui state associated with the domain and most of the reducers have a similar structure. It looks something like this :</p>

<pre><code>export default function home(state = initialState, action = {}) {
  switch (action.type) {
    case OPEN:
      return {
        ...state,
        ui: {
          ...state.ui,
          [action.key]: true
        }
      };
    case CLOSE:
      return {
        ...state,
        ui: {
          ...state.ui,
          [action.key]: false
        }
      };
    case SELECT_TAB:
      return {
        ...state,
        ui: {
          selectedTab: action.selected
        }
      };
    default:
      return state;
  }
}
</code></pre>

<p>We end up having the same actions repeated over and over for the ui, mainly toggles and setting what is displayed. Is there a way to keep the domain based ui within a reducer without having to add in the <code>OPEN</code> and <code>CLOSE</code> type statements for the ui in every reducer. Maybe what I'm thinking of is an anti pattern in redux but curious to see if anyone has run into this kind of issue before?</p>

<p><strong>UPDATE:</strong></p>

<p>I like the solution listed below but how would you extend the reducer to sort of contain a ui reducer within it. </p>

<pre><code>combineReducers({ 
    home: {createUiReducer('home', initialState), ...home}
})
</code></pre>

<p>That way you can have nested domain based ui without repeating all of the actions. Not sure how you would go about that because you would essentially be dynamically adding CASE statements.</p>