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

@nekogd/react-utility-hooks

v1.2.9

Published

Modular utility hooks that we often use grouped in one package. Written in TypeScript, documented, tested and maintained.Disclaimer: at least React 16 is needed (the one with hooks ;) )

Downloads

6

Readme

React Utility Hooks

Modular utility hooks that we often use grouped in one package.
Written in TypeScript, documented (NPM and JSDoc), unit tested (not all of them atm) and maintained.
Disclaimer: at least React 16 is needed (that's the one with hooks)).:)

The example usages are in JavaScript.

For TS users, please refer to API or JSDoc of a given hook, you have all the necessary information about types.

Live demo (React app that makes use of the package)

CodeSandbox: https://codesandbox.io/s/github/nekogd/react-utility-hooks-examples-sandbox

GitHub: https://github.com/nekogd/react-utility-hooks-examples-sandbox

useWhyRerender

Happy debugging!
The hook to inspect why the component has rerendered (we've all been (or will be) there).
If we have changes in component props, we will have them logged into js console.
This helps us a lot in debugging.

Example usage

import { useWhyRerender } from '@nekogd/react-utility-hooks';

const ExampleComponent = React.memo((props) => {
  const { count, style } = props;
  useWhyRerender('example component name', props);

  return <div style={style}>{count}</div>;
});

Check your JS console, it will be empty if props has not changed, or will show you the changes.

API

type IUseWhyRerender = {
  [key: string]: any;
};

function useWhyRerender(componentName: string, props: IUseWhyRerender): void;

useEventListener

If we find ourselves adding eventListeners with useEffect a lot, it might be a good idea to abstract that to a custom hook.

More info: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Example usage

import { useEventListener } from '@nekogd/react-utility-hooks';

const ExampleComponent = () => {
  // Initial state to track down mouse position
  const [coords, setCoords] = useState({ x: 0, y: 0 });

  // Make sure the reference don't change
  const handler = useCallback(
    ({ clientX, clientY }) => {
      setCoords({ x: clientX, y: clientY });
    },
    [setCoords],
  );

  // Add event listener using our hook
  useEventListener('mousemove', handler);

  return (
    <>
      <h2>client mouse coords</h2>
      <table>
        <tbody>
          <tr>
            <td>clientX</td>
            <td>{coords.x}</td>
          </tr>
          <tr>
            <td>clientY</td>
            <td>{coords.y}</td>
          </tr>
        </tbody>
      </table>
    </>
  );
};

Api

type Target =
  | (() => HTMLElement)
  | HTMLElement
  | React.MutableRefObject<HTMLElement>
  | Window;

function useEventListener(
  eventName: string,
  handler: Function,
  options?: { target: Target; capture?: boolean; once?: boolean; passive?: boolean },
): void;

useClickAway

A hook that manages click outside of target elements.

Example usage

  import { useClickAway } from "@nekogd/react-utility-hooks";

  const ExampleComponent = () => {
    const [counter, setCounter] = useState(0);
    const ref = useRef();
    
    // with TypeScript we might have to say const ref = useRef() as React.MutableRefObject<HTMLElement>; 
    
    useClickAway(() => {
      setCounter((s) => s + 1);
    }, ref);

    return (
      <>
        <div>
          Click anywhere outside the span to increse the counter
          <span ref={ref}>
            <button type="button">This does not increase the counter</button>
          </span>
          <p>counter: {counter}</p>
        </div>
      </>
    );
  }

API

function useClickAway(
  onClickAway: (event: MouseEvent | TouchEvent) => void,
  target: (() => HTMLElement) | HTMLElement | React.MutableRefObject,
);

Params

| Property | Description | Type | Default | | ----------- | ------------------------- | ------------------------------------------------------------ | ------- | | onClickAway | Trigger Function | (event) => void | - | | target | DOM element or Ref Object | (() => HTMLElement) | HTMLElement | React.MutableRefObject | - |

useHover

Inspect if component is hovered.

Example usage

import { useHover } from '@nekogd/react-utility-hooks';

const ExampleComponent = () => {
  const [hoverRef, isHovered] = useHover();

  return <div ref={hoverRef}> {isHovered ? 'I am hovered' : 'Not hovered'}</div>;
};

API


function useHover(<T extends HTMLElement>(): [
  (node?: T | null) => void,
  boolean,
])

useDocumentTitle

Change document title without React Helmet. :)

Example usage

import { useDocumentTitle } from '@nekogd/react-utility-hooks';

const ExampleComponent = () => {
  useDocumentTitle(`my new Document Title`);

  return <> Content of my component </>;
};

API

function useDocumentTitle(title: string): void;

useSlug

Generate slug from input string.

Example usage

import { useSlug } from '@nekogd/react-utility-hooks';

const ExampleComponent = () => {
  const slug = useSlug('Some string');

  return <> The slug is {slug} </>;
};

API

function useSlug(input: string): string | null;

UseToggle

Just to toggle i.e. accordions. Accepts initial boolean value.

example usage

const ExampleComponent = () => {
  const { toggled, handleToggled } = useToggle();

  const handleClick = () => {
    handleToggled();
  };

  return (
    <>
      <button onClick={handleClick}>toggle</button>
      {toggled ? 'visible' : 'hidden'}
    </>
  );
};

API

type IUseToggle = { toggled: boolean; handleToggled: (value?: boolean) => void };

function useToggle(initialState: boolean = false): IUseToggle;

useCounter

Groundbreaking useCounter example to give understanding of the flow of this package i.e. types and tests.

Example usage

import { useCounter } from "@nekogd/react-utility-hooks";

const ExampleComponent = () => {
  const { count, increment, reset, decrement } = useCounter();
  return (
    <>
      <button onClick={increment}>Increment counter</button>
      <button onClick={reset}>Reset counter</button>
      <button onClick={decrement}>Decrement counter</button>
      <p>{count}</p>
    </>
  );
};

API

type IUseCounter = {
  count: number;
  increment: () => void;
  reset: () => void;
  decrement: () => void;
};

function useCounter(initialValue: number = 0): IUseCounter;