Launch Week Day 3: Introducing Organization Notifications in Socket.Learn More
Socket
Book a DemoSign in
Socket

@brunoparga/react-combine-reducers

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brunoparga/react-combine-reducers

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

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

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

Install the dependency using the following command:

npm install react-combine-reducers

If using typescript, you can also install the types:

npm install @types/react-combine-reducers

Usage

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

  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

  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

  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

  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.

Give us a star if this helped you! Cheers!

Keywords

react

FAQs

Package last updated on 14 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts