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-create-array-context

v0.1.0

Published

Efficiently use React Context with arrays

Downloads

4

Readme

React Create Array Context

Efficiently use React Context with arrays

Motivation

The React Context API has a hidden second argument called calculateChangedBits, which you can use as a performance optimization to reduce unecessary renders to context consumers. However, using this API involes knowledge of bitwise operators which makes it tricky to use. This library implements the details of calculateChangedBits (and unstable_observedBits) for you, assuming your context state is an array.

To be honest, I'm not sure this library is useful. If you're experiencing performance issues with React Context, consider using some of the solutions suggested here before reaching for this library.

Getting Started

Install

npm install --save react-create-array-context

# Or yarn
# yarn add react-create-array-context

Basic Usage

const [ArrayContextProvider, useArrayContext] = createArrayContext();

// This component will rerender _only_ when the 0th element
// of the context array changes (initially 'substate0')
function ArrayContextConsumer0() {
  const [contextState, setContextState] = useArrayContext([0]);

  const handleChange = (e) => {
    const { value } = e.taraget;
    // 🚨 You must use the callback version when setting
    // 🚨 state because `contextState` may be stale (since it
    // 🚨 doesn't update when 1st or 2nd element change)
    setContextState(arr => {
      const copy = [ ...arr ];
      copy[0] = value;
      return copy;
    })
  }

  return <input onChange={handleChange} value={contextState[0]} />
}

// This component will rerender _only_ when the 0th or 1st element
// of the context array changes
function ArrayContextConsumer12() {
  const [contextState, setContextState] = useArrayContext([1, 2]);

  // Do something with contextState[0] and contextState[1]
}

// This component will rerender when any element of the context array changes
function ArrayContextConsumer() {
  const [contextState, setContextState] = useArrayContext();

  // Do something with contextState
}

function App() {
  <ArrayContextProvider initialState={['substate0', 'substate1', 'substate2']}>
    <ArrayContextConsumer0 />
    <ArrayContextConsumer12 />
    <ArrayContextConsumer />
  </ArrayContextProvider>
}

Caveats

  1. You must use the callback version of setting state (see 🚨 emoji above).
  2. Because of calculateChangedBits only has 32 bits to work with, arrays longer than 32 elements may trigger unnecessary rerenders (but it will still be more efficient than not using this library).