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

@cruise-automation/hooks

v0.0.1

Published

A set of resusable React hooks

Downloads

31

Readme

@cruise-automation/hooks

A set of resusable React hooks.

Install

npm install --save @cruise-automation/hooks

Hooks

useAbortable

A React hook to load async, disposable resources and fire the cleanup callback when the component unmountts. If the component unmounts before the async operation completes, the resource will still be cleaned up once it finishes loading and an abort signal will be issued to the async load operation.

// types
 useAbortable<T>(
  defaultValue: T,  // default value, will be set to the actual value if action is performed successfully
  action: (AbortController) => Promise<T>, // async action to perform
  cleanup: (?T) => void, // clean up work if thtere is any
  args: any // dependencies
): [T, () => void] // result value and abort function
// sample usage
import { useAbortable } from "@cruise-automation/hooks";

function Example(props) {
  const [data, setData] = useState(null);
  const dateName = 'foo';
  // fetch data from remote when the component is mounted
  const [remoteData, abortFn] = useAbortable(
    null,
    async (abortController) =>  {
      if (dataName) {
        fetchDataFromRemote(props, dataName)
      }
    }
    (val) => {},
    [dataName]
  );

  // abort is usually called automatically when the component unmounts, but it can also be called manually
  function abortManually() {
    abortFn();
  }

  return (
    <div>
      <button onClick={abortManually}>Stop Loading</button>
    </div>
  );
}

useCleanup

A small React hook to fire the cleanup callback when the component unmounts. Equivalent to useEffect(() => () => { teardown(); }, []).

// types
 useCleanup(teardown: () => void): void
// sample usage
function Example() {
  const [audioContext] = React.useState(() => new window.AudioContext));
  // automatically close audioContext when the component unmounts
  useCleanup(() => audioContext.close());
  return null;
}

useEventListener

A hook for conditionally adding and removing event listeners on DOM elements.

// types
useEventListener<T>(
  target: Element, // event target, e.g. window
  type: string, // event type
  enable: boolean,
  handler: (any) => void,
  dependencies: any[],
): void
// sample usage
function Example() {
  const [isDragging, setIsDragging] = useState(false);
  useEventListener(
    window,
    "mousemove",
    isDragging, // add this listener only during mouse dragging
    (event) => {
      /* do something here... */
    },
    []
  );
  return <div onMouseDown={() => setIsDragging(true)}>demo</div>;
}

useAnimationFrame

A React hook that accepts a callback function which will be called repeatedly, synchronized with the browser's repaints via requestAnimationFrame.

// types
useAnimationFrame(
  callback: (timestamp: number) => void,
  disable: boolean,
  dependencies: any[],
): void
// sample usage: a count state that gets updated in every animation frame
function Example() {
  const [count, setCount] = React.useState(0);
  useAnimationFrame(
    (timestamp) => {
      setCount(count + 1);
    },
    false,
    []
  );
  return <span>{count}</span>;
}