Addcase cannot be called with two reducers for the same action type

When you receive the error message “addcase cannot be called with two reducers for the same action type”, it means that you are trying to define multiple reducers for the same action type in your Redux application. In Redux, each action type should have only one corresponding reducer.

To understand this issue better, let’s consider an example. Suppose you have an action type called “ADD_CASE” which is responsible for adding a new case to your application. You might have defined the following two reducers:


// Reducer 1
const reducer1 = (state, action) => {
  switch (action.type) {
    case 'ADD_CASE':
      // Logic to update state for adding a new case
      return newState;
    default:
      return state;
  }
};

// Reducer 2
const reducer2 = (state, action) => {
  switch (action.type) {
    case 'ADD_CASE':
      // Logic to perform some other operation for adding a new case
      return newState;
    default:
      return state;
  }
};

In the above example, we have two different reducers, reducer1 and reducer2, both trying to handle the same “ADD_CASE” action type. This will result in the mentioned error, as Redux doesn’t allow multiple reducers for the same action type.

To resolve this issue, you need to refactor your reducers so that each action type has only one corresponding reducer. One way to achieve this is by combining the logic of both reducers into a single reducer:


const reducer = (state, action) => {
  switch (action.type) {
    case 'ADD_CASE':
      // Logic to update state for adding a new case
      // Logic to perform some other operation for adding a new case
      return newState;
    default:
      return state;
  }
};

By combining the logic of both reducers into a single reducer, you ensure that the “ADD_CASE” action type is handled by only one reducer, eliminating the error.

Read more interesting post

Leave a comment