# chainReducers

Creates a reducer that chains together a sequence of (sub-)reducers.

```js
// JavaScript

function chainReducers(firstChildReducer, ...otherChildReducers)
```

```ts
// TypeScript

function chainReducers<S = any, A extends Action = AnyAction>(
  firstChildReducer: Reducer<S, A>,
  ...otherChildReducers: SubReducer<S, A>[]
): Reducer<S, A>
```

## Details

Given a sequence of (sub-)reducers, `chainReducers` creates a reducer which forwards incoming actions to each of these "child" reducers. More specifically, the reducer:

1. calls the first child reducer with the received state
2. passes the resulting state (and the same action) to the next reducer
3. repeats step 2 until reaching the last reducer, whose returned state is finally returned by the `chainReducers` reducer.

Note that the first child reducer in the chain is the only one which needs to handle an `undefined` state by returning the initial state; all others can be *sub-reducers* - such as the ones returned by [`onAction`](/api/onaction.md) - which assume that they are only ever called with a defined, already-initialized state.

## Examples

Chaining `withInitialState` and `onAction` reducers:

```js
import {
  chainReducers,
  onAction,
  withInitialState
} from 'redux-preboiled'

const reducer = chainReducers(
  withInitialState(0),
  onAction('increment', state => state + 1),
  onAction('multiply', (state, { payload }) => state * payload)
)

reducer(undefined, { type: '' })
// => 0

reducer(0, { type: 'increment' })
// => 1

reducer(2, { type: 'multiply', payload: 4 })
// => 8
```

## See Also

* [Reducers](/guides/reducers.md) guide
* [onAction](/api/onaction.md)
* [withInitialState](/api/withinitialstate.md)


---

# 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://redux-preboiled.js.org/api/chainreducers.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.
