
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
composable-reducer
Advanced tools
Create composable reducers on the fly from objects
First and foremost, composableReducer is a helper that lets developers create reducers on the fly from objects and their key => value pairs, where keys are the dispatch types, thus reducing the boilerplate you have to write while creating reducers to use with React's useReducer hook.
Each function returns only the piece of state it wants to modify. I call these micro-reducers.
Each micro-reducer can be used inside a composed-reducer (hah! composable-reducer, who would've guessed?) creating a chain. These are just reducers that when called call their micro-reducers in the order they're defined. Every micro-reducer receives the full state mixed with the updated bits created by the previous micro-reducers, this way every micro-reducer can have access to an up-to-date state.
Throw in the mix the possibility to define aliases for micro-reducers and composed-reducers, the possibility to pass static arguments around (e.g. addOne: 'add({ "amount": 1 })', where add is just a micro-reducer), and you have a pretty powerful utility in your hands☝🏼. The best part about the static-arguments thing? They have the highest priority (i.e. they can't be overridden), can be used in aliases for composed-reducers, and are thrown away after being used, so they don't pollute your micro-reducer chain!
☝🏼: Seriously, you can extract all the business logic from a component and add features just by adding micro-reducers and composed-reducers, give it a try!
object's keys and values.dispatch type is not recognized.$ npm install composable-reducer
Or if you prefer using Yarn:
$ yarn add composable-reducer
import { composableReducer } from "composable-reducer"
const reducer = composableReducer({
setDiscountPercentage: (_, { percentage }) => ({
percentage,
}),
percentage: ({ percentage, price }) => ({
discount: (percentage / 100) * price,
}),
decrease: ({ discount, price }) => ({
discountedPrice: price - discount,
}),
setLabel: (_, { label }) => ({
label,
}),
setDiscountedPrice: [
"setDiscountPercentage",
"percentage",
"decrease",
'setLabel({ "label": "Sales!" })',
],
})
reducer({ price: 50 }, { type: "setDiscountedPrice", percentage: 20 })
// => { price: 50, percentage: 20, discount: 10, discountedPrice: 40, label: 'Sales!' }
const Component = ({ price }) => {
const [{ price, discountedPrice = price }, dispatch] = useReducer(reducer, {
price: price,
})
useTimeout(
() => dispatch({ type: "setDiscountedPrice", percentage: 20 }),
5000
)
return (
<>
{discountedPrice < price && <del>{price}</del>} {discountedPrice}
</>
)
}
Create a reducer from object by using its key => value pairs, where keys are the dispatch types and values are their handlers. Read more about micro-reducers and composed-reducers above.
Type: object
Type: object
Type: string
Default Value: "type"
The dispatch property that should be used to distinguish between different dispatches, by default it's "type", e.g. dispatch({ type: 'add' }).
The latest version of Chrome, Firefox, Safari, and Edge. I think that Edge 12 is the lowest you can go.
FAQs
Create composable reducers on the fly from objects
We found that composable-reducer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.