# Use with Reducers

Handling actions dispatched by Redux promise middleware is simple by default.

```javascript
const FOO_TYPE = 'FOO';

// Dispatch the action
const fooActionCreator = () => ({
  type: FOO_TYPE
  payload: Promise.resolve('foo')
});

// Handle the action
const fooReducer = (state = {}, action) => {
  switch(action.type) {
    case `${FOO_TYPE}_PENDING`:
      return;

    case `${FOO_TYPE}_FULFILLED`:
      return {
        isFulfilled: true,
        data: action.payload
      };

    case `${FOO_TYPE}_REJECTED`:
      return {
        isRejected: true,
        error: action.payload
      };

    default: return state;
  }
}
```

### Action Types

Optionally, the default promise suffixes can be imported from this module.

```javascript
import { ActionType } from 'redux-promise-middleware';
```

This can be useful in your reducers to ensure types are more robust.

```javascript
const FOO_PENDING = `FOO_${ActionType.Pending}`;
const FOO_FULFILLED = `FOO_${ActionType.Fulfilled}`;
const FOO_REJECTED = `FOO_${ActionType.Rejected}`;
```

## Large Applications

In a large application with many async actions, having many reducers with this same structure can grow redundant.

To keep your reducers [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself), you might see value in using a solution like [type-to-reducer](https://github.com/tomatau/type-to-reducer).

```javascript
import typeToReducer from 'type-to-reducer';

const BAR_TYPE = 'BAR';

// Dispatch the action
const barActionCreator = () => ({
  type: BAR_TYPE
  payload: Promise.resolve('bar')
});

// Handle the action
const barReducer = typeToReducer({
    PENDING: () => ({
      // ...
    }),
    REJECTED: (state, action) => ({
      isRejected: true,
      error: action.payload
    }),
    FULFILLED: (state, action) => ({
      isFulfilled: true,
      data: action.payload
    })
  }
}, {});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pburtchaell.gitbook.io/redux-promise-middleware/guides/reducers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
