
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.
react-handler-hooks
Advanced tools
React hooks for persistent and parameterizable callbacks - useCallback on steroids!
React hooks for persistent and parameterizable callbacks - useCallback on steroids!
npm i react-handler-hooks
yarn add react-handler-hooks
Two hooks that fully replace the useCallback hook: useHandler and useParamsHandler. And they have many benefits!
Works and looks just like a normal useCallback, but you don't need to put dependencies.
useHandler(() => {
// no params
});
useHandler((event: React.MouseEvent) => {
// single param
});
useHandler((num: number, str: string) => {
// two params etc...
});
const [clicks, setClicks] = useState<number>(0);
const [timestamp, setTimestamp] = useState<number>(0);
const onClick = useHandler((event: React.MouseEvent) => {
// no need for function (clicks => clicks + 1) in setter
// nor adding clicks to dep list!
setClicks(clicks + 1);
setTimestamp(Math.floor(event.timeStamp));
});
return (
<h1 onClick={onClick}>
Click count: {clicks} ({timestamp})
</h1>
);
Allows for passing parameters that don't originate from the original callback. First parameter is a key or object with your params that has a key property. The hook returns a callback creator which returns the same callback for each unique key.
useParamsHandler((userId: string | number) => {
// 1 passed param (which is the key)
});
useParamsHandler((params: { key: number, str: string }) => {
// passed params object if you need multiple params
});
useParamsHandler((params: { key: string, num: number }, e: React.MouseEvent) => {
// passed params and callback params
});
useParamsHandler((key: number, usersIds: string[], data: any) => {
// 1 passed param and 2 callback params
});
const [clickedName, setClickedName] = useState<string>();
const [timestamp, setTimestamp] = useState<number>(0);
const onClick = useParamsHandler((name: string, event: React.MouseEvent) => {
setClickedName(name);
setTimestamp(Math.floor(event.timeStamp));
});
return (
<>
<h1 onClick={onClick('Ariel')}>
Ariel
</h1>
<h1 onClick={onClick('John')}>
John
</h1>
<h1 onClick={onClick('Mary')}>
Mary
</h1>
{clickedName && `Clicked on ${clickedName} at ${timestamp}!`}
</>
);
Both hooks support an optional second parameter with a DependencyList that is just like the DependencyList in useEffect, useLayoutEffect or useCallback.
This is only useful when you actually do want to get a brand new function into your child components. A very good examples are render props.
These hooks still have advantages over useCallback even if you're using a deplist, but they're mainly for you to not switch back between useCallback and these hooks.
Check out the benchmark folder.
E-mail: ariel.jurkowski@gmail.com
Send me a nice message if you're using this!
FAQs
React hooks for persistent and parameterizable callbacks - useCallback on steroids!
We found that react-handler-hooks 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.