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-use-auto-memo

v0.1.0

Published

useMemo for objects and arrays with automatic dependencies

Downloads

8

Readme

react-use-auto-memo

useMemo for objects and arrays with automatic dependencies. Used with contexts to reduce re-renders.

Installation

npm i react-use-auto-memo

Features

  • useAutoMemo accepts an object or an array and automatically creates a dependency array based on the object/array
  • Small bundle size
  • Written in TypeScript
  • Descriptive error messages

Examples

With object

const useCount = () => {
    const [count, setCount] = useState(0);

    // use auto memo so that the containing object does not change until either count or setCount does
    const memoisedObject = useAutoMemo({ count, setCount });
    // this is equivalent to useMemo(() => ({ count, setCount }), [count, setCount])
    
    return memoisedObject;
};

With array

import { useCallback } from "react";
import useAutoMemo from "./useAutoMemo";

const useMyAmazingHook = () => {
    const [names, setNames] = useState<string[]>([]);

    const sortedNames = useMemo(() => [...names].sort(), [names]);
    const addName = useCallback((name: string) => setNames(names => [...names, name]), [setNames]);

    // use auto memo so that the containing array does not change until either sortedNames or addName does
    return useAutoMemo([sortedNames, addName]);
}

But why?

The intended use case for this hook is to memoise the value provided by a React context.

const Context = createContext({});

const MyContextProvider = ({ children }: MyContextProviderProps) => {
    const [names, setNames] = useState<string[]>([]);

    const sortedNames = useMemo(() => [...names].sort(), [names]);
    const addName = useCallback((name: string) => setNames(names => [...names, name]), [setNames]);
    
    const contextValue = useAutoMemo({ sortedNames, addName });
    
    return <Context.Provider value={contextValue}>{children}</Context.Provider>
}

The idea is to stop the container object or array (passed to useAutoMemo) from changing when it does not have to. If we just did <Context.Provider value={{ sortedNames, addName }}>..., we will create a new object on every render and all consumers of the context will have to re-render. By first wrapping it in useAutoMemo, the container object will only change when one or more of its properties change. This, of course, requires all of its properties or elements to be the same (reference equal) on each render and they should therefore be wrapped in useMemo, useCallback or similar.

Be careful

  • This hook only works for objects and arrays
  • The dependency array of the underlying useMemo is created from the object's values (Object.values) or the array itself. This means that the number of properties in an object or elements in an array must not change between renders.

If you want to reduce boilerplate when creating React contexts, check out react-super-context which is the library that inspired me to create this.