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

recontextual

v0.2.0

Published

This is a library built upon [`use-context-selector`](https://github.com/dai-shi/use-context-selector).

Downloads

347

Readme

Recontextual: simple, easy-to-type, selectable contexts

This is a library built upon use-context-selector.

It has a simpler API and is easier to type and select from.

Install

Simply install the package:

npm install --save recontextual

Example

import * as React from 'react';
import recontextualize from '../.';

interface IExample {
  value: string;
  setValue: (newValue: string) => void;
}

const [ExampleProvider, useExample] = recontextualize<IExample>();

function FirstComponent() {
  const { value, setValue } = useExample(
    ({ value, setValue }) => ({ value, setValue }),
    true
  );
  return <input value={value} onChange={evt => setValue(evt.target.value)} />;
}
function SecondComponent() {
  const value = useExample(({ value }) => value);
  return <h1>Second, value is: {value}</h1>;
}

function App() {
  const [value, set] = React.useState('initial');
  const contextValue = {
    value,
    setValue: (newValue: string) => set(newValue),
  };
  return (
    <ExampleProvider value={contextValue}>
      <FirstComponent />
      <SecondComponent />
    </ExampleProvider>
  );
}

export default App;

API Reference

recontextualize

function recontextualize<IType>(): [Provider<IType>, Selector<IType>];

recontextualize is the default export and takes no arguments, but should be given a type argument.

The function returns an array of two items: A context provider, and a context selector hook.

The returned context provider can be used as any other context provider, by supplying a value property that conforms to the type argument. Components consuming the context must be wrapped in a provider, or they will throw a runtime error. No default value can be set.

The returned context selector hook can be invoked with 0, 1, or 2 arguments and returns the whole context or a selection from the context. However, if the context has changed, but the selection from the context has not, the hook will not cause a component render but rather abort the render loop.

As an example, imagine we have this:

interface ISomething { ... };
const [SomethingProvider, useSomething] = recontextualize<ISomething>();

This context selector hook, useSomething has the following three call signatures:

function useSomething(): ISomething;
function useSomething<U>(selector: (ctx: ISomething) => U): U;
function useSomething<U>(selector: (ctx: ISomething) => U, isMulti: boolean): U;

If useSomething is invoked without arguments, the hook returns the nearest provided context value as-is. Use this variant to select the entire context.

If useSomething is invoked with a single selector argument or with a selector argument and isMulti=false, the hook returns the value returned by the selector argument, but only if the value has changed using strict equality. If the selector return value is unchanged, this hook aborts the component render (as long as this is the only changed hook of course). Use this variant to select a single value from the context.

If useSomething is invoked with a selector argument and isMulti=true, the hook returns the object returned by the selector argument only if the object has changed using shallow equality on the values of the object. If the selector return value is shallowly unchanged, this hook aborts the component render (as long as this is the only changed hook of course). Use this variant to select multiple values from the context.