---
title: "Reducer"
slug: "reducer"
updated: 2024-03-21T11:01:30Z
published: 2024-03-21T11:01:30Z
canonical: "docs.mapp.com/reducer"
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mapp.com/llms.txt
> Use this file to discover all available pages before exploring further.
# Reducer
Components with many state updates spread across many event handlers can get overwhelming. For these cases, you can consolidate all the state update logic outside your component in a single function, called a *reducer*.
## Reducer with custom actions
| Value | Description | Data type | Required |
| --- | --- | --- | --- |
| actions | Extends your actions and executes custom tracking code after dispatching your action. | `Object` | - |
| arguments0 | Your current state. | `Object` | - |
| arguments1 | Your current action. | `Object` | - |
| arguments2 | Value of your reducer function. | `Object` | - |
### Integration
```javascript
import React, { useReducer } from "react";
import { WebtrekkSmartPixelReact, webtrekkReducer } from "@webtrekk-smart-pixel/react";
```
```javascript
import React, { useReducer } from "react";
import { WebtrekkSmartPixelReact, webtrekkReducer } from "@webtrekk-smart-pixel/next";
```
```javascript
const webtrekkCustomReducer = webtrekkReducer({
"increment": (state, action, reducerValue) => {
WebtrekkSmartPixelReact.action({
name: action.type,
parameter: {1: reducerValue.count + ""}
});
WebtrekkSmartPixelReact.trackAction();
},
"decrement": (state, action, reducerValue) => {
WebtrekkSmartPixelReact.action({
name: action.type,
parameter: {1: reducerValue.count + ""}
});
WebtrekkSmartPixelReact.trackAction();
}
});
const initialState = {count: 0};
const reducer = (state, action) => {
switch (action.type) {
case "increment": return {count: state.count + 1};
case "decrement": return {count: state.count - 1};
default: throw new Error();
}
};
const Counter = () => {
const [state, dispatch] = useReducer(webtrekkCustomReducer(reducer), initialState);
return (
Count: { state.count }
);
};
class CounterButton extends React.Component {
render() {
return (
);
}
}
export default CounterButton;
```
## Dispatch with tracking data
| Value | Description | Data type | Required | Default value |
| --- | --- | --- | --- | --- |
| webtrekk.type | Defines which type of tracking data you are adding to the tracking pixel. You can use one of the following types: | `String` | Yes | |
| `webtrekk/init` |
| `webtrekk/advanced` |
| `webtrekk/page` |
| `webtrekk/action` |
| `webtrekk/session` |
| `webtrekk/campaign` |
| `webtrekk/customer` |
| `webtrekk/product` |
| `webtrekk/products` |
| `webtrekk/order` |
| `webtrekk/extension` |
| `webtrekk/track` |
| `webtrekk/trackPage` |
| `webtrekk/trackAction` |
| webtrekk.data | Defines which data you are adding to the tracking pixel. | `Any` | Yes | |
| `webtrekk/init`. See [init](/v1/docs/tracking-configuration-init). |
| `webtrekk/advanced`. See [advanced](/v1/docs/react-next-advanced). |
| `webtrekk/page`. See [page](/v1/docs/react-next-page). |
| `webtrekk/action`. See [action](/v1/docs/react-next-action). |
| `webtrekk/session`. See [session](/v1/docs/react-next-session). |
| `webtrekk/campaign`. See [campaign](/v1/docs/react-next-campaign). |
| `webtrekk/customer`. See [customer](/v1/docs/react-next-customer). |
| `webtrekk/product`. See [product](/v1/docs/react-next-product). |
| `webtrekk/products`. See [products](/v1/docs/react-next-product). |
| `webtrekk/order`. See [order](/v1/docs/react-next-order). |
| `webtrekk/track`. See [track](/v1/docs/react-next-track). |
| `webtrekk/trackPage`. See [trackPage](/v1/docs/react-next-trackpage). |
| `webtrekk/trackAction`. See [trackAction](/v1/docs/react-next-trackaction). |
| webtrekk.sendInstantly | If set to `true` , all added data is instantly tracked (you must not call `track`). | `Boolean` | | `false` |
### Integration
```javascript
import React, { useReducer } from "react";
import { webtrekkReducer } from "@webtrekk-smart-pixel/react";
```
```javascript
import React, { useReducer } from "react";
import { webtrekkReducer } from "@webtrekk-smart-pixel/next";
```
```javascript
const webtrekkCustomReducer = webtrekkReducer();
const initialState = {count: 0};
const reducer = (state, action) => {
switch (action.type) {
case "increment": return {count: state.count + 1};
case "decrement": return {count: state.count - 1};
default: throw new Error();
}
};
const Counter = () => {
const [state, dispatch] = useReducer(webtrekkCustomReducer(reducer), initialState);
return (
Count: { state.count }
);
};
class CounterButton extends React.Component {
render() {
return (
);
}
}
export default CounterButton;
```