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

@ekwoka/preact-global-state

v2.0.0

Published

Global State for Preact. With Types!

Downloads

348

Readme

Peact Global State

Preact Global State is a simple state management library for Preact, enabling the sharing of state between components, similar to React's Context API, or other libraries like Redux. The big benefit of Preact Global State over these alternatives is the ease-of use and surgical precision (improved even further with the use of signals instead of classic state).

This project began as a rewrite of preact-global-state but has since been dramatically altered and extended. With 2.0.0, the core utilizes @preact/signals (and requires it as a peer). This does increase the overall footprint of adding this to a non-signal project, but reduces the footprint when already using signals.

If you are not using signals and just want a global equivalent to useState, you can install 1.0.1 instead.

Installation

pnpm add @ekwoka/preact-global-state # for signals
pnpm add @ekwoka/[email protected]  # for non-signals projects

Usage

Classic state

const [counter, setCounter] = useGlobalState<number>('my-counter', 0); // (state label: string; initial value?: any)

return (
  <div>
    <button onClick={() => setCounter(1)}> /* directly setting the state Set to 1! */</button>
    <button onClick={() => setCounter((prev) => prev + 1)}> /* using a state function to update the state Increment! */</button>
  </div>
);

And in another

const [counter] = useGlobalState<number>('my-counter', 0);

return (
  <div>
    <p>{counter}</p>
  </div>
);

Using signals

const count = useGlobalSignal<number>('my-counter', 0); // (state label: string; initial value?: any)

return (
  <div>
    <button onClick={() => count.value++}> /* Incrementing the signal value */ </button>
  </div>
);

And in another

const count = useGlobalSignal<number>('my-counter', 0);

return (
  <div>
    <p>{count}</p>
  </div>
);

This state can be shared among any number of components, and the use of useGlobalState and useGlobalSignal can be mixed! This allows you to get the benefits of a global signal while also having state updaters to pass to external libraries, or to mix legacy components with newer signals based components.

Store

One consideration of these hooks is that the first calling of a hook with a specific default value will set that value to the state. Subsequent initializations will ignore the default value.

While smart usage can ensure that this is not an issue, it could be the cause of any issues.

To avoid this, or to simply enable an explicit initialization of the global state, you can use the Store function outside of components (or at some root level).

Store({
  hello: 'world',
  foo: 'bar',
  blazeit: 420
});