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

@utilityjs/create-store-context

v1.1.0

Published

A React store-context that manages states of a tree where unnecessary re-renders have been omitted thanks to the Pub/Sub design pattern.

Downloads

3

Readme

A React store-context that manages states of a tree where unnecessary re-renders have been omitted thanks to the Pub/Sub design pattern.

license npm latest package npm downloads types

npm i @utilityjs/create-store-context | yarn add @utilityjs/create-store-context

How does it work?

The <StoreProvider> works as a Publisher and each useStore is a Subscriber. So when the state changes, only necessary Subscribers will be re-rendered. Since we are using useRef instead of useState the Publisher itself won't re-render.

This way we guarantee that only the necessary components need to be re-rendered.

Usage

import createStoreContext from "@utilityjs/create-store-context";

interface StoreState {
  count: number;
  log: () => void;
  increase: (amount: number) => void;
}

const { useStore, StoreProvider } = createStoreContext<StoreState>(
  (setState, getState) => ({
    count: 0,
    log: () => void getState().count,
    increase: amount =>
      setState(state => ({ ...state, count: state.count + amount }))
  })
);

const Controls = () => {
  const increase = useStore(state => state.increase);

  return (
    <div>
      <button onClick={() => increase(1)}>Increase by 1</button>
      <button onClick={() => increase(5)}>Increase by 5</button>
      <button onClick={() => increase(10)}>Increase by 10</button>
    </div>
  );
};

const Display = () => {
  const { count, log } = useStore(state => ({
    count: state.count,
    log: state.log
  }));

  log();

  return <div>Count: {count}</div>;
};

const Container = () => (
  <div>
    <h2>Container</h2>
    <Controls />
    <Display />
  </div>
);

const App = () => {
  return (
    <main id="main">
      <div id="main-wrapper">
        <StoreProvider>
          <h1>App</h1>
          <Container />
        </StoreProvider>
      </div>
    </main>
  );
};

API

createStoreContext(stateFactory)

type StateSelector<State, PartialState> = (
  store: State
) => PartialState;

type UseStoreHook<State> = <PartialState = State>(
  selector: StateSelector<State, PartialState>
) => PartialState;

type StateFactory<S> = (
  setState: (setter: (prevState: S) => S) => void,
  getState: () => S
) => S;

declare const createStoreContext: <S>(stateFactory: StateFactory<S>) => {
  StoreProvider: (props: { children: React.ReactNode }) => JSX.Element;
  useStore: UseStoreHook<S>;
};

stateFactory

An initialization function to initialize the states.