# react-combine-reducers

> A helper utility to apply combineReducers functionality in React useReducer hook for large scale applications

Latest version **1.1.1** (published 2021-01-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-combine-reducers
pnpm add react-combine-reducers
yarn add react-combine-reducers
bun add react-combine-reducers
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2021-01-18 |
| First published | 2019-12-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/react-combine-reducers) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 81 |
| Author | Ankita Chakraborty |
| Maintainers | teamninja |
| Keywords | react, react-hooks, reducer, combine, reducers, hooks |

## Links

- npm: https://www.npmjs.com/package/react-combine-reducers
- Repository: https://github.com/ankita1010/react-combine-reducers
- Homepage: https://github.com/ankita1010/react-combine-reducers#readme
- Issues: https://github.com/ankita1010/react-combine-reducers/issues
- npm.io page: https://npm.io/package/react-combine-reducers

## Dependencies (1)

- [core-js](https://npm.io/package/core-js.md) ^3.6.5

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2021-01-18
- 1.0.4 — 2020-07-23
- 1.0.3 — 2020-03-15
- 1.0.2 — 2020-01-09
- 1.0.1 — 2019-12-17
- 1.0.0 — 2019-12-06

## README

# React Combine Reducers

This module contains a handy `combineReducers` function to use in conjunction with the React hook `useReducer`.

You may use it with **JavaScript** or **TypeScript**.


## Installation

[![npm version](https://badge.fury.io/js/react-combine-reducers.svg)](https://badge.fury.io/js/react-combine-reducers)

Install the dependency using the following command:
```bash
npm install react-combine-reducers
```

If using typescript, you can also install the types:
```bash
npm install @types/react-combine-reducers
```

## Usage

Once installed, import the helper function and use it like so:

```javascript
  import { useReducer } from 'react';
  import combineReducers from 'react-combine-reducers';

  const [reducerCombined, initialStateCombined] = combineReducers({
    reducerOne: [reducerOne, initialStateOne],
    reducerTwo: [reducerTwo, initialStateTwo],
    // ...
  });
```

To avoid the initial instantiation call you can use the `useCallback` hook from
react

```javascript
  import { useReducer, useCallback } from 'react';
  import combineReducers from 'react-combine-reducers';

  const [reducerCombined, initialStateCombined] = useCallback(
    combineReducers({
      reducerOne: [reducerOne, initialStateOne],
      reducerTwo: [reducerTwo, initialStateTwo],
      // ...
    }), 
    [reducerCombined, initialStateCombined]
   );
```

### Working Javascript Example

```javascript
  import { useReducer } from 'react';
  import combineReducers from 'react-combine-reducers';

  const initialIdentity = {
    name: 'Harry'
  }

  const initialLocation = {
    country: 'UK',
    city: 'London'
  }

  const identityReducer = (state, action) => {
    switch (action.type) {
      case 'ACTION_A':
        return { ...state, name: 'Puli' };
      default: return state;
    }
  }

  const locationReducer = (state, action) => {
    switch (action.type) {
      case 'ACTION_B':
        return { ...state, city: 'Manchester' };
      default: return state;
    }
  }

  const [profileReducer, initialProfile] = combineReducers({
    identity: [identityReducer, initialIdentity],
    location: [locationReducer, initialLocation]
  });

  const [state, dispatch] = useReducer(profileReducer, initialProfile);

  console.log(state);
  // Outputs the following state:
  // {
  //   identity: {
  //     name: "Harry"
  //   },
  //   location: {
  //     country: "UK",
  //     city: "London"
  //   }
  // }
```

### Working Typescript Example

```typescript
  import { useReducer } from 'react';
  import combineReducers from 'react-combine-reducers';

  type Identity = {
    name: string;
  };

  type Location = {
    country: string;
    city: string;
  };

  type ProfileState = {
    identity: Identity;
    location: Location;
  };

  type Action = {
    type: string;
    payload: any;
  };

  type ProfileReducer = (state: ProfileState, action: Action) => ProfileState;

  const initialIdentity: Identity = {
    name: 'Harry'
  };

  const initialLocation: Location = {
    country: 'UK',
    city: 'London'
  };

  const identityReducer = (state: Identity, action: Action) => {
    switch (action.type) {
      case 'ACTION_A':
        return { ...state, name: 'Puli' };
      default:
        return state;
    }
  };

  const locationReducer = (state: Location, action: Action) => {
    switch (action.type) {
      case 'ACTION_B':
        return { ...state, city: 'Manchester' };
      default:
        return state;
    }
  };

  const [profileReducer, initialProfile] = combineReducers<ProfileReducer>({
    identity: [identityReducer, initialIdentity],
    location: [locationReducer, initialLocation]
  });

  const [state, dispatch] = useReducer<ProfileReducer>(
    profileReducer,
    initialProfile
  );

  console.log(state);
  // Outputs the following state:
  // {
  //   identity: {
  //     name: "Harry"
  //   },
  //   location: {
  //     country: "UK",
  //     city: "London"
  //   }
  // }
```


## Issues:
If you find a bug, please file an issue on [our issue tracker on GitHub](https://github.com/ankita1010/react-combine-reducers/issues).

Give us a star if this helped you!
Cheers!

---
_Source: https://npm.io/package/react-combine-reducers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
