All Projects → SanCoder-Q → flat-combine-reducers

SanCoder-Q / flat-combine-reducers

Licence: ISC license
Turns multiple reducer functions, into a single reducer function.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to flat-combine-reducers

Use Substate
🍙 Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (+592.86%)
Mutual labels:  reducer
react-redux-spinner
An automatic spinner for react and redux
Stars: ✭ 81 (+478.57%)
Mutual labels:  reducer
flow-state
UI state management with RxJS.
Stars: ✭ 33 (+135.71%)
Mutual labels:  reducer
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (+664.29%)
Mutual labels:  reducer
redux-entities
Higher-order reducer for store entities received from normalizr and makes it easy to handle them.
Stars: ✭ 34 (+142.86%)
Mutual labels:  reducer
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (+128.57%)
Mutual labels:  reducer
Xstateful
A wrapper for xstate that stores state, handles transitions, emits events for state changes and actions/activities, and includes an optional reducer framework for updating state and invoking side-effects
Stars: ✭ 81 (+478.57%)
Mutual labels:  reducer
redux-recompose
A Redux utility belt for reducers and actions. Inspired by acdlite/recompose.
Stars: ✭ 70 (+400%)
Mutual labels:  reducer
Redux
No description or website provided.
Stars: ✭ 40 (+185.71%)
Mutual labels:  reducer
redux-fp
Functional programming helpers for Redux.
Stars: ✭ 28 (+100%)
Mutual labels:  reducer
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (+964.29%)
Mutual labels:  reducer
redux-tools
💪 Maintaining large Redux applications with ease.
Stars: ✭ 34 (+142.86%)
Mutual labels:  reducer
redux-interactions
Composing UI as a set of interactions
Stars: ✭ 22 (+57.14%)
Mutual labels:  reducer
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (+628.57%)
Mutual labels:  reducer
redux-leaves
Write once. Reduce anywhere.
Stars: ✭ 52 (+271.43%)
Mutual labels:  reducer
React Hook Thunk Reducer
📡 A React useReducer() hook whose dispatcher supports thunks à la redux-thunk.
Stars: ✭ 91 (+550%)
Mutual labels:  reducer
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (+0%)
Mutual labels:  reducer
comby-reducer
A simple program reducer for any language.
Stars: ✭ 65 (+364.29%)
Mutual labels:  reducer
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (+7.14%)
Mutual labels:  reducer
redux-lightweight
Write one business logic instead of writing actions, action types and reducers
Stars: ✭ 41 (+192.86%)
Mutual labels:  reducer

flat-combine-reducers

build status npm npm Codacy Badge codecov

Dependency Status devDependency Status Greenkeeper badge semantic-release

Turns multiple reducer functions, into a single reducer function, with the support of declaring the initial states as default parameters.

It will call all the child reducers, and gather their results into a single state object by assign the result objects together from left to right. The reason for the name is it flatten the state of using the combineReducers in redux.

npm install --save flat-combine-reducers

Why

To prevent the reducer growing too large, Redux provides a function called combineReducers to let us structure the reducer. However, it will structure not only the reducer itself but also your state. Each state that managed by distinct reducer will be separated into a deep object with a specified key. Moreover, the combining logic should ideally focus on the combining only when creating the store. And each reducer should manage their own states as the default values of the first parameter. flatCombineReducers allow us to give the initial state as default parameters. Thus, it could put the responsibility of each reducer in order.

Options (Optional)

You can pass an object to its last parameter to control the flatCombineReducers' behavior.

const options = { mergePrevState: false }
flatCombineReducers(reducerA, reducerB, options)
OptionName DefaultValue Description
mergePrevState true If true, the combined reducer will assign the returned state with the previous state.

mergePrevState

With its help, you can simplify your reducer logic and avoid the boilerplate likes Object.assign({}, state, PROCESSED_STATE)

From:

function reducerA(state = initialState, action) {
  switch (action.type) {
    case ACTION_TYPE_A:
      return Object.assign({}, state, {
        visibilityFilter: action.filter
      });
    default:
      return state;
  }
}

To:

function reducerA(state = initialState, action) {
  switch (action.type) {
    case ACTION_TYPE_A:
      return {
        visibilityFilter: action.filter
      };
  }
}

Examples

Return identity reducer if parameter is empty

const reducer = flatCombineReducers();

expect(reducer({ A: 1, B: 2 }, "indifferentAction")).to.deep.equal({ A: 1, B: 2 });
expect(reducer({ A: 1, B: 2 })).to.deep.equal({ A: 1, B: 2 });
expect(reducer()).to.deep.equal({});

Note. You can change it by specifying the option mergePrevState.

Combines multiple reducers into one

const reducer = flatCombineReducers(
  (prevState, action) => ({A: prevState.A + action.value}),
  (prevState, action) => ({B: prevState.B * action.value})
);

expect(reducer({ A: 1, B: 2 }, { value: 3 })).to.deep.equal({ A: 4, B: 6 });

Supports separated initial state values as default parameters

const reducer = flatCombineReducers(
  (prevState = { A: 1 }, action) => ({A: prevState.A + action.value}),
  (prevState = { B: 2 }, action) => ({B: prevState.B * action.value})
);

expect(reducer(undefined, { value: 3 })).to.deep.equal({ A: 4, B: 6 });

Assigns the states from right to left

const reducer = flatCombineReducers(
  (prevState = { A: 1 }, action) => ({...prevState, A: prevState.A + action.value}),
  (prevState = { A: 2 }, action) => ({...prevState, A: prevState.A * action.value})
);

expect(reducer(undefined, { value: 3 })).to.deep.equal({ A: 6 });

Specify the option mergePrevState to control the behavior

const reducer = flatCombineReducers(
  (prevState, action) => ({A: prevState.A + action.value}),
  { mergePrevState: false }
);

expect(reducer({ A: 1, B: 2 }, { value: 3 })).to.deep.equal({ A: 4 });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].