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-noui/external-store

v2.0.0

Published

Create external stores for your react projects

Downloads

4

Readme

@react-noui/external-store

Simple external stores for your control flow.

Installation

With yarn

yarn add @react-noui/external-store

With npm

npm install --save @react-noui/external-store

Basic Usage

You can use any primitive, and most complex types with makeExternalStore(defaultValue).

// src/stores.ts
import { makeExternalStore } from "@react-noui/external-store";
export const BoolStore = makeExternalStore(false);
export const StringStore = makeExternalStore("");
export const NumberStore = makeExternalStore(0);
export const MapStore = makeExternalStore(new Map<string, number>());
export const SetStore = makeExternalStore(new Set<string>());
export const RecordStore = makeExternalStore({ foo: 0, bar: "" });
export const ArrayStore = makeExternalStore<string>([]);
export const RegExpStore = makeExternalStore(new RegExp("", "gi"));

Advanced Usage

More complex operations can use specialized makeExternalStore{SPECIALTY} functions.

LocalStorage, SessionStorage access

makeExternalStoreStorage<T>

Syncing with Storage types in React can be conflated with unintended effects. This library offers a solution when dealing with Storage on the DOM.

Make a storage singleton.

// src/stores.ts
import { makeExternalStoreStorage } from "@react-noui/external-store";

export const MySetting = makeExternalStoreStorage(localStorage, "my_setting", false);

In MySetting example, we check if localStorage.get("my_setting") exists.

  • If it does, then we will use the existing value.
  • If it does not then we will set localStorage to the provided defaultValue.

Using MySetting singleton store in a React component:

// src/components/MySettingCheckbox.tsx
import { MySetting } from "../stores";

export const MySettingCheckbox = () => (
  <input
    type={checkbox}
    value={MySetting.useValue()}
    onChange={(event) => MySetting.setValue(event.target.checked)}
  />
)

Custom Events

makeExternalStoreCustomEvent<T>

You can store event data from any event type that you expect.

// src/stores.ts
import { makeExternalStoreCustomEvent } from "@react-noui/external-store";

const FooListener = makeExternalStoreCustomEvent("event__foo", { bar: 1 });

const FooListenerNow = makeExternalStoreCustomEvent("event__foo", { bar: 1 }, { autoListen: true });

The difference between FooListenerNow and FooListener is the timing of adding an event listener.

FooListener will register a listener for "event__foo" ONLY when the hook FooListener.useValue() is called in a React component.

If your event can happen before a component renders (ie: race condition), FooListenerNow will register a listener immediately upon creation. This means your React components can have event data that was emitted before rendering.

autoListen is opt-in to preserve memory and performance. With this option, an event listener is always listening, and updating FooListenerNow.

By default, an event listener is only added when the FooListener.useValue() hook is rendered.