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

@victorzimnikov/utility-hooks

v0.6.2

Published

Collection of low-level React hooks.

Downloads

4

Readme

utility-hooks

Collection of low-level React hooks.

Build codecov npm version npm minzipped size npm type definitions npm downloads npm license

Installation

npm install @victorzimnikov/utility-hooks

Environment compatibility

utility-hooks output uses modern browser features, all extra transpilations and polyfills should be done in application side.

Static checking with react-hooks/exhaustive-deps

 {
-  "react-hooks/exhaustive-deps": ["warn"]
+  "react-hooks/exhaustive-deps": [
+    "warn",
+    {
+      "additionalHooks": "^(useMemoWith|usePromise|usePureDeps|usePureMemo|useIsomorphicLayoutEffect)$"
+    }
+  ]
 }

Hooks

useEventCallback(callback)

Inspired by How to read an often-changing value from useCallback?

Unlike useCallback, useEventCallback does not accept second argument and stores original callback in ref.

 function Form() {
   const [text, updateText] = useState("");
-  const textRef = useRef();
-
-  useEffect(() => {
-    textRef.current = text; // Write it to the ref
-  });
-
-  const handleSubmit = useCallback(() => {
-    const currentText = textRef.current; // Read it from the ref
-    alert(currentText);
-  }, [textRef]); // Don't recreate handleSubmit like [text] would do
+  const handleSubmit = useEventCallback(() => {
+    alert(text);
+  });

   return (
     <>
       <input value={text} onChange={e => updateText(e.target.value)} />
       <ExpensiveTree onSubmit={handleSubmit} />
     </>
   );
 }

useIsomorphicLayoutEffect(effect, deps)

Inspired by react-redux/src/utils/useIsomorphicLayoutEffect

Runs useLayoutEffect in browser environment (checks document.createElement), otherwise useEffect.

useConstant(factory)`

Inspired by How to create expensive objects lazily?

Runs factory only once and writes value in component ref.

 function Image(props) {
-  const ref = useRef(null);
   const node = useRef();
-
-  // ✅ IntersectionObserver is created lazily once
-  function getObserver() {
-    let observer = ref.current;
-    if (observer !== null) {
-      return observer;
-    }
-    let newObserver = new IntersectionObserver(onIntersect);
-    ref.current = newObserver;
-    return newObserver;
-  }
+  const observer = useConstant(() => new IntersectionObserver(onIntersect));

   useEffect(() => {
-    getObserver().observe(node.current);
+    observer.observe(node.current);
   }, [observer]);
 }

useMemoWith(factory, deps, isEqual)

Inspired by Gist.

Compares each dependency with isEqual function to memoize value from factory.

 export function useFetch(url, options) {
-  const cachedOptionsRef = useRef();
-
-  if (
-    !cachedOptionsRef.current ||
-    !_.isEqual(options, cachedOptionsRef.current)
-  ) {
-    cachedOptionsRef.current = options;
-  }
+  const cachedOptions = useMemoWith(() => options, [options], _.isEqual);

   useEffect(() => {
     // Perform fetch
-  }, [url, cachedOptionsRef.current]);
+  }, [url, cachedOptions]);
 }

usePrevious(value)

Inspired by How to get the previous props or state?

Stores value used in previous render.

 function Counter() {
-  const prevCountRef = useRef();
   const [count, setCount] = useState(0);
-
-  useEffect(() => {
-    prevCountRef.current = count;
-  });
+  const prevCount = usePrevious(count);

   return (
     <h1>
-      Now: {count}, before: {prevCountRef.current}
+      Now: {count}, before: {prevCount}
     </h1>
   );
 }

usePromise(factory, deps)

Handles loading of promises created by factory function.

const [filter, setFilter] = useState('')
- const [value, setValue] = useState();
- const [error, setError] = useState()
- useEffect(() => {
-   const controller = new AbortController();
-   const runEffect = async () => {
-     try {
-       const value = await fetch(
-         "https://foo.bars/api?filter=" + filter,
-         { signal: controller.signal }
-       );
-
-       setValue(value);
-     } catch (error) {
-       if (err.name === 'AbortError') {
-         console.log("Request was canceled via controller.abort");
-         return;
-       }
-
-       setError(error)
-     }
-   };
-   runEffect();
-   return () => controller.abort()
- }, [filter]);
+ const { value, error } = usePromise(({ abortSignal }) => fetch(
+  "https://foo.bars/api?filter=" + filter,
+   { signal: abortSignal }
+ ), [filter])

usePureMemo(deps, isEqual)

Returns next deps only when they were changed based on isEqual result.

usePureMemo(factory, deps, isEqual)

Works like useMemoWith, but also compares return value.

 export function useFetch(url, options) {
-  const cachedOptionsRef = useRef();
-
-  if (
-    !cachedOptionsRef.current ||
-    !_.isEqual(options, cachedOptionsRef.current)
-  ) {
-    cachedOptionsRef.current = options;
-  }
+  const cachedOptions = usePureMemo(() => options, [options], _.isEqual);

   useEffect(() => {
     // Perform fetch
-  }, [url, cachedOptionsRef.current]);
+  }, [url, cachedOptions]);
 }

useValueRef(value)

Inspired by How to read an often-changing value from useCallback?

Works like useRef, but keeps it's ref in sync with value on every call.

function Form() {
  const [text, updateText] = useState('');
+  const textRef = useValueRef(text);
-  const textRef = useRef();
-
- useEffect(() => {
-   textRef.current = text; // Write it to the ref
- });

  const handleSubmit = useCallback(() => {
    const currentText = textRef.current; // Read it from the ref
    alert(currentText);
  }, [textRef]); // Don't recreate handleSubmit like [text] would do

  return (
    <>
      <input value={text} onChange={e => updateText(e.target.value)} />
      <ExpensiveTree onSubmit={handleSubmit} />
    </>
  );
}

useWhenValueChanges(value, effect, isEqual)

Works like useEffect, but runs effect only when value compared by isEqual (Object.is if not provided). It also passes the previous value as an effect argument.

function List({ disptach, page, selectedId }) {
-  const isInitial = useRef(true);
  useEffect(() => {
-    isInitial.current = true;
    dispatch({ type: "FETCH_LIST", page });
  }, [page, dispatch]);
  useEffect(() => {
    dispatch({ type: "FETCH_ITEM", id: selectedId });
  }, [selectedId, dispatch]);
-  useEffect(() => {
-    if (isInitial.current) {
-      isInitial.current = false;
-    } else if (!selectedId) {
-      dispatch({ type: "FETCH_LIST", page });
-    }
-  }, [page, selectedId, dispatch]);
+  useWhenValueChanges(selectedId, () => {
+    if (!selectedId) {
+      dispatch({ type: "FETCH_LIST", page });
+    }
+  });
}

Utilities

areDepsEqualWith(hookName, nextDeps, prevDeps, isEqual)

Compares each dependency with isEqual function.