Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
reduce-reducers
Advanced tools
The reduce-reducers npm package allows you to combine multiple reducer functions into a single reducer function. This is particularly useful in state management scenarios, such as with Redux, where you may want to split the logic for handling different parts of the state into separate reducers and then combine them.
Combining Multiple Reducers
This feature allows you to combine multiple reducers into a single reducer function. The code sample demonstrates how to combine a countReducer and a textReducer into a rootReducer using reduce-reducers.
const { combineReducers } = require('redux');
const reduceReducers = require('reduce-reducers');
const initialState = { count: 0, text: '' };
const countReducer = (state = initialState.count, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
const textReducer = (state = initialState.text, action) => {
switch (action.type) {
case 'SET_TEXT':
return action.text;
default:
return state;
}
};
const rootReducer = reduceReducers(
(state = initialState, action) => state,
combineReducers({ count: countReducer, text: textReducer })
);
console.log(rootReducer(undefined, { type: 'INCREMENT' })); // { count: 1, text: '' }
console.log(rootReducer(undefined, { type: 'SET_TEXT', text: 'Hello' })); // { count: 0, text: 'Hello' }
Redux is a popular state management library for JavaScript applications. It provides a combineReducers function that allows you to combine multiple reducers into a single reducer. While reduce-reducers focuses on combining reducers in a more flexible way, Redux's combineReducers is more straightforward and is a core part of the Redux library.
Redux Loop is an alternative to redux-thunk and redux-saga. It allows you to handle side effects in your reducers. While it doesn't directly compete with reduce-reducers, it offers a different approach to managing complex state logic and side effects in Redux applications.
Reducer Composer is another library that allows you to combine multiple reducers into one. It provides a similar functionality to reduce-reducers but with a different API. It focuses on composing reducers in a more functional programming style.
Reduce multiple reducers into a single reducer from left to right
npm install reduce-reducers
import reduceReducers from 'reduce-reducers';
const reducer = reduceReducers(
(prev, curr) => ({ ...prev, A: prev.A + curr }),
(prev, curr) => ({ ...prev, B: prev.B * curr })
);
expect(reducer({ A: 1, B: 2 }, 3)).to.deep.equal({ A: 4, B: 6 });
expect(reducer({ A: 5, B: 8 }, 13)).to.deep.equal({ A: 18, B: 104 });
Originally created to combine multiple Redux reducers that correspond to different actions (e.g. like this). Technically works with any reducer, not just with Redux, though I don't know of any other use cases.
reduceReducers
and combineReducers
?Take a look at this StackOverflow post: https://stackoverflow.com/questions/48104778/how-does-reducereducers-work
FAQs
Reduce multiple reducers into a single reducer
The npm package reduce-reducers receives a total of 341,209 weekly downloads. As such, reduce-reducers popularity was classified as popular.
We found that reduce-reducers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.