> For the complete documentation index, see [llms.txt](https://react-observatory.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://react-observatory.js.org/dynamic-injection/reducers.md).

# Reducers

This chapter covers the basic setup for Dynamic Reducer Injection.

Please note that reducers are not removed from Store when Component is unmounted.

## Global Reducer Setup

First, we need to create a function that will allow us to inject new reducers dynamically.

In this example, the `counter` reducer will be available on every page.

```javascript
import { composeReducerCreator } from '@react-observatory/inject-reducer'

const counter = (state = 0, action) => {
  return state + 1;
}

export default composeReducerCreator({
  counter
})
```

## Store Configuration

After that, we need to enhance the store with applyAsyncReducers.

It needs the `reducerCreator` to replace the old reducer with an updated one when we inject new reducers dynamically.

```javascript
import { createStore } from 'redux'
import { applyAsyncReducers } from '@react-observatory/inject-reduers'
import reducerCreator from './reducers'

const store = createStore(
  reducerCreator(),
  initialState,
  applyAsyncReducers(reducerCreator)
);
```

## Dynamic Reducer Injection inside the Components

Now we can start injecting reducers dynamically, the reducer for `TaskList` component will be loaded along with React Component.

```javascript
import { injectReducer } from '@react-observatory/inject-reducer'
import taskListReducer from './reducers'
import TaskList from './TaskList'

const withReducer = injectReducer('taskList', taskListReducer)

export default withReducer(TaskList)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://react-observatory.js.org/dynamic-injection/reducers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
