
Research
Supply Chain Attack on Axios Pulls Malicious Dependency from npm
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.
use-undo-reducer
Advanced tools
A tiny React hook for adding undo/redo to reducer-driven state, with support for history limits, keybindings, and replay-based performance optimization.
npm install use-undo-reducer
or
yarn add use-undo-reducer
import { useUndoRedoReducer } from 'use-undo-reducer';
type State = { count: number };
type Action = { type: 'inc' } | { type: 'dec' };
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case 'inc': return { count: state.count + 1 };
case 'dec': return { count: state.count - 1 };
default: return state;
}
};
function Counter() {
const { state, dispatch, undo, redo } = useUndoRedoReducer(reducer, { count: 0 }, {
keybinds: {
undo: e => e.metaKey && !e.shiftKey && e.key === 'z',
redo: e => (e.metaKey && e.shiftKey && e.key === 'z') || (e.ctrlKey && e.key === 'y'),
},
historyLimit: 50,
onUndoFailed: () => alert('No more undos!'),
onRedoFailed: () => alert('Nothing to redo!'),
});
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => dispatch({ type: 'dec' })}>-</button>
<button onClick={() => dispatch({ type: 'inc' })}>+</button>
<button onClick={undo}>Undo</button>
<button onClick={redo}>Redo</button>
</div>
);
}
This hook is designed for situations where state changes over time and you want to let the user undo or redo previous actions — like drawing tools, diagram editors, form builders, etc.
Instead of storing full snapshots of state, we store an action history, then replay the reducer from a base projection whenever the user undoes or redoes. This ensures:
function useUndoRedoReducer<S, A>(
reducer: (state: S, action: A) => S,
initialState: S,
config?: {
historyLimit?: number;
keybinds?: {
undo: (e: KeyboardEvent) => boolean;
redo: (e: KeyboardEvent) => boolean;
};
onUndoFailed?: () => void;
onRedoFailed?: () => void;
}
): {
state: S;
dispatch: (action: A) => void;
undo: () => void;
redo: () => void;
}
| Name | Type | Description |
|---|---|---|
reducer | (state: S, action: A) => S | Your standard reducer function |
initialState | S | The initial state for your reducer |
config | (optional) object | Configuration object |
| Key | Type | Default | Description |
|---|---|---|---|
historyLimit | number | 100 | Max number of actions to keep in history |
keybinds.undo | (e: KeyboardEvent) => boolean | undefined | Function that returns true if the keypress should trigger undo |
keybinds.redo | (e: KeyboardEvent) => boolean | undefined | Function that returns true if the keypress should trigger redo |
onUndoFailed | () => void | undefined | Called when undo is not possible |
onRedoFailed | () => void | undefined | Called when redo is not possible |
| Key | Type | Description |
|---|---|---|
state | S | The current computed state |
dispatch | (action: A) => void | Dispatch a new action |
undo | () => void | Undo the previous action |
redo | () => void | Redo a previously undone action |
You must provide keybinds as functions that return true if the event matches.
keybinds: {
undo: e => e.metaKey && e.key === 'z' && !e.shiftKey,
redo: e => e.metaKey && e.key === 'z' && e.shiftKey,
}
This gives you full control over how to handle platforms (Windows vs macOS), keyboard layouts, or app-specific shortcuts.
group() support) — this is a potential future feature.Pull requests, issues, and feedback are very welcome!
This project was built with care to solve real-world complex undo/redo in React-based visual editors.
MIT © Sam Apostel
FAQs
Robust Undo & Redo functionality as simple as a React.useReducer
We found that use-undo-reducer demonstrated a healthy version release cadence and project activity because the last version was released less than 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.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.

Security News
TeamPCP is partnering with ransomware group Vect to turn open source supply chain attacks on tools like Trivy and LiteLLM into large-scale ransomware operations.