npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-handler-hooks

v2.1.0

Published

React hooks for persistent and parameterizable callbacks - useCallback on steroids!

Downloads

17

Readme

🐱 react-handler-hooks

React hooks for persistent and parameterizable callbacks - useCallback on steroids!

Build

Installation

npm i react-handler-hooks
yarn add react-handler-hooks

What are these?

Two hooks that fully replace the useCallback hook: useHandler and useParamsHandler. And they have many benefits!

Pros

  • Hooks will return the same function on each render, this means children will never rerender because of a callback change.
  • No dependency list needed, because hooks always use fresh callbacks internally. Your state values from useState will always be fresh in your callbacks.
  • Supports passed parameters and they can even be dynamic.
  • Full type safety with TypeScript.
  • Optional dependency list, useful for render props.
  • useHandler is fully reverse compatible with useCallback.
  • Cleaner code, easier debugging, less headaches!

Cons

  • useParamsHandler needs a unique key. You need to be cautious with it, just like with the key property for JSX elements.
  • The hooks by themselves are technically (but barely) slower than useCallback, but they will give you the lost (and much more) performance back in lack of re-renders.

useHandler

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>
);

useParamsHandler

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}!`}
  </>
);

DependencyList

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.

Benchmark

Check out the benchmark folder.

Contact

E-mail: [email protected]
Send me a nice message if you're using this!

License

MIT