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-signal-state

v0.0.2

Published

When we can't have SolidJS at home

Downloads

4

Readme

React Signal State

MOME, can we get SolidJS

No. There is SolidJS at home.

SolidJS at home:

Installation

pnpm add react-signal-state

Main APIs

useSignalState

import { useSignalState } from "react-signal-state";
import { signal } from "@preact/signals-core";

const counter = signal(0);
setInterval(() => (counter.value = counter.value + 1), 1000);

function Counter() {
  const $ = useSignalState({ count: counter });
  return <div>{$.count}</div>;
}

createSignalStore

import { createSignalStore } from "react-signal-state/store";
import { signal, computed } from "@preact/signals-core";

const store = createSignalStore(({ initialName }: { initialName: string }) => {
  const name = signal(initialName);
  const showedName = computed(() => (name.value === "React" ? "Solid" : name.value));

  return {
    $: { name, showedName },
    changeName: (newName: string) => (name.value = newName),
  };
});

function App() {
  return (
    <store.provider initialName="React">
      <Name />
    </store.provider>
  );
}

function Name() {
  const { $, changeName } = store.useSignalState();
  return (
    <div>
      <input value={$.name} onChange={(e) => changeName(e.target.value)} />
      <div>{$.showedName}</div>
    </div>
  );
}

Why

Making complex state in React is hard. State is attached to the component tree, and it's difficult to model complex and shared state between components. Hooks like useState and useContext are great, but have a mental model that is hard (or tedious) to scale.

That why many state solutions exist for React, like Redux, Tanstack, Jotai and Zuztand. or even similar alternatives or compilers, like Preact and Million.js. It's a fundamental problem in a great ecosystem.

Event thought the great advances in developer experience, there is space to improve. The idea of this library, at the current state, is to explore different patterns that seems to be more unexplored, using Signals and ideas from SolidJS.

Capabilities

See the example code.

  • Smart subscriptions: Instead of a selection like pattern, like:

    const stateA = useStore((state) => state.a);

    This library tracks automatically witch signal is accessed and subscribe to it, using a proxy.

    function WillOnlyRunOnAChange() {
      const $ = useSignalState({ a: signal(1), b: signal(2) });
      console.log($.a); // Will only run when $.a changes.
    }
  • Great composition: Signals are already composable. This library also allows to compose via context.

    const storeA = createSignalStore(() => {
      const a = signal(1);
      return { $: { a } };
    });
    const storeB = createSignalStore(() => {
      const { a } = storeA.useStore();
      const b = computed(() => a.value + 1);
      return { $: { b } };
    });
  • Incredible small.

  • Supports SSR. Doesn't need hydration helpers, it just works.

  • Does not require any code transformation.

Notes and remaining things to explore

  • Should this library use @preact/signals-core? Is there another signal library that is better?
  • This readme probably should be rewritten.
  • Ths useStore inside createSignalStore is weird. But is it ilegal? See if React's use is more appropriate.
  • Tracking with effects is hacky. An implementation with an API like Watcher in the Signal Proposal might be preferable.