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

@vtaits/react-signals

v1.0.1

Published

React integration for TC39 signals

Downloads

55

Readme

NPM dependencies status

@vtaits/react-signals

This is an integration of react and TC39 signals

Motivation

This library not only integrates react with the proposed signals but also alleviates some state-management cases in react components:

  • Access to current state value in the callback

    Without signals:

    const [value, setValue] = useState(0);
    
    const someCallback = useCallback(async () => {
      await someAsyncAction();
    
      // how to get an actual value???
    }, [value]);

    With signals:

    const value = useSignalState(0);
    
    const someCallback = useCallback(async () => {
      const valueBeforeAction = value.get();
    
      await someAsyncAction();
    
      // You can get it easyly
      const valueAfterAction = value.get();
    }, [value]);
  • Callbacks that depend on signals are unchanged:

    Without signals:

    const [value, setValue] = useState(0);
    
    // Create new callback on every value change
    const someCallback = useCallback(async () => {
      // ...
    }, [value]);

    With signals:

    const value = useSignalState(0);
    
    // The value refers to the same signal
    const someCallback = useCallback(async () => {
      // ...
    }, [value]);
  • Re-render only the leaf component when prop drilling

    function InnerComponent({ counter }) {
      useRerender([counter]);
    
      return (
        <div>
          {counter.get()}
        </div>
      );
    }
    
    function OuterComponent({ counter }) {
      return <InnerComponent signal={counter} />
    }
    
    function RootComponent() {
      const counter = useSignalState(0);
    
      return (
        <>
          <OuterComponent counter={counter} />
    
          <button
            type="button"
            onClick={() => {
              counter.set(counter.get() + 1);
            }}
          >
            Increase
          </button>
        </>
      );
    }

    Only the InnerComponent component is re-rendered after clicking the increase button.

API

useSignalState

Create a signal as a local state of the component. This is an alternative to useState.

import { useRerender, useSignalState } from "@vtaits/react-signals";

function App() {
  const counter = useSignalState(0);

  useRerender([counter]);

  return (
    <>
      <span>{counter.get()}</span>

      <button
        type="button"
        onClick={() => {
          counter.set(counter.get() + 1);
        }}
      >
        Increase
      </button>
    </>
  );
}

NOTE that unlike useState, the component will not re-render automatically. Therefore, if you want to bind component rendering to the signal, you have to call useRerender.

useSignalComputed

Create a computed signal. This is an alternative to useMemo.

import { useRerender, useSignalComputed, useSignalState } from "@vtaits/react-signals";

function App() {
  const counter = useSignalState(0);
  const isEven = useSignalComputed(() => (counter.get() & 1) == 0);
  const parity = useSignalComputed(() => isEven.get() ? "even" : "odd");

  useRerender([counter, parity]);

  return (
    <>
      <div>{counter.get()}</div>
      <div>{parity.get()}</div>

      <button
        type="button"
        onClick={() => {
          counter.set(counter.get() + 1);
        }}
      >
        Increase
      </button>
    </>
  );
}

NOTE that unlike useMemo, the component will not re-render automatically. Therefore, if you want to bind component rendering to the signal, you have to call useRerender.

useRerender

Bind component rendering to the signal. The signal can be both local and external.

The example with a global signal:

import { useRerender } from "@vtaits/react-signals";
import { Signal } from "signal-polyfill";

const clock = new Signal.State(new Date()); // Create a global signal that stores the current date

setInterval(() => {
	clock.set(new Date());
}, 100);

export function App() {
	useRerender([clock]); // Bind rendering to global signal

	return (
    <div>
      {new Intl.DateTimeFormat("en-US", {
        dateStyle: undefined,
        timeStyle: "medium",
        hour12: false,
      }).format(clock.get())}
    </div>
	);
}

useSignalEffect

This is an alternative to useEffect. This hook automatically tracks changes to signals within the callback.

import { useSignalEffect, useSignalState } from "@vtaits/react-signals";

export function App() {
	const counter = useSignalState(0);

	useSignalEffect(() => {
		const counterValue = counter.get();

		// init logic

		return () => {
		  // destroy logic
		};
	}, []);

	return (
    <button
      type="button"
      onClick={() => {
        counter.set(counter.get() + 1);
      }}
    >
      Increase
    </button>
  );
}

NOTE that this hook tracks automatically only signals. If you want also to track some non-signal values, you can use the second argument like to useEffect.

const log = useLogger();

useSignalEffect(() => {
  const counterValue = counter.get();

  log(counterValue);
}, [log]);

useSignalify

Wrap non-signal value to the signal to allow to use it with signal-flow.

import { useSignalify } from "@vtaits/react-signals";

function App({
  id,
}: {
  id: string;
}) {
  const idSignal = useSignalify(id);

  // ...
}

Local development

  • npm run storybook - start storybook with examples
  • npm lint - run linter
  • npm test:ts - check typescript
  • npm test:storybook - run storybook tests
  • npm test - run all validators
  • npm lint:fix - auto-correct some linter errors
  • npm test:storybook:dev - run storybook tests in watch mode