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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tommostools/use-context-selector

v0.3.0

Published

Hook for simple context slicing in React

Readme

use-context-selector

npm size

Hook for simple context slicing in React


Introduction

A simple utility function in the style of Dai Shi's https://www.npmjs.com/package/use-context-selector which allows for React components to ignore irrelevant changes in context values, reducing unnecessary re-renders.

The stock React Context implementation causes any change in value to re-render all components that use that context. The Contexto library provides an alternative implementation which uses a pub-sub design to allow components to selectively subscribe only to relevant value updates.

use-context-selector provides a simple hook useContextSelector() to be used with Contexto contexts for the simple case of subscribing only to changes in a single member within the Contexto value, and the common case of an value computed directly from the Contexto value:

useContextSelector(ContextoContext, "keyof-Context-value");

useContextSelector(ContextoContext, (contextValue) => someFunction(contextValue));

For more elaborate context data manipulations, and an integrated caching system, you may wish to look at into contextors, which also build on the Contexto library.

Usage

import { createContext } from "contexto";
import { useContextSelector } from "@tommostools/use-context-selector";

const MyContext = createContext({
  nestedValue: { inner: "some value" },
  array: ["one", "two", "three"],
  foo: 123,
});

const Consumer = ({ offset }) =>
  {
    // `value1` updates only when the context's `.nestedValue` changes
    const value1 = useContextSelector(MyContext, (contextValue) => contextValue.nestedValue);

    // `value2` updates only when the length of the array changes
    const value2 = useContextSelector(MyContext, (contextValue) => contextValue.array.length);

    // `value3` subscribes only to the value of `.foo`, using a convenient syntax
    const value3 = useContextSelector(MyContext, "foo");

    // `value4` updates when `.foo` changes, but the selector has a dependency on `offset`
    const value4 = useContextSelector(
        MyContext,
        (contextValue) => contextValue.foo + offset,
        [offset]);

    return <div>{`${value1} / ${value2} / ${value3} / ${value4}`}</div>
  }

render(<Consumer/>);

Dependencies

When specifying a selector function, useContextSelector expects a third argument containing a list of values that the function's stability depends on, in the style of useMemo or useCallback. The selector function will be updated and re-evaluated when any of the dependencies changes.

The dependency list default to the empty list if omitted, so the values of any local variables referenced in the selector will remain unchanged after the component is mounted:

const Consumer = () =>
  {
    const [tick, setTick] = useState(0);
    useEffect(() => setInterval(setTick(t => t + 1), 1000), []);

    // `tick` will always be 0
    const valueWithoutDeps = useContextSelector(
      SomeContext,
      (value) => `${value}: ${tick}`
    );

    // `tick` will continuously increase
    const valueWithDeps    = useContextSelector(
      SomeContext,
      (value) => `${value}: ${tick}`,
      [tick]
    );

    return <div>{`${valueWithoutDeps} / ${valueWithDeps}`}</div>
  }

Installation

Contexto, and thus use-context-selector, is compatible with React 16.8+, React Native 16.8+ and Preact 10+.

Once you've installed Contexto, you can just:

npm install use-context-selector

or

yarn add use-context-selector

use-context-selector comes with its own TypeScript definitions.