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

@projectnatz/react-signals

v1.0.1

Published

Utilize the power of signal state management in your React project!

Downloads

10

Readme

React Signals

Utilize the power of signal state management in your React project!

Installation

npm i react-signals

Both React and React Native are supported.

Create a Signal

Create a signal from a value or initializer function using signal().

import { signal } from "react-signals";

const counter = signal(0);

counter.value++;

console.log("Counter value: " + counter.value); // Counter value: 1

Every signal is lazy by defualt, so any initializer function passed is only called when the signal value is requested.

import { signal } from "react-signals";

function expensiveCalculation()
{
	// ...
}

const mySignal = signal(expensiveCalculation); // <- expensiveCalculation is not executed yet

console.log("Signal value: " + mySignal.value); // <- expensiveCalculation is executed and the value returned
console.log("Signal value again: " + mySignal.value); // <- the value previously calculated value is returned

You can listen to signal changes and react on them. A cleanup function is returned after each subscription request.

import { signal } from "react-signals";

const counter = signal(0);
const unsubscribe = counter.subscribe(() => console.log("Signal value changed to: " + counter.value));

counter.value++; // Signal value changed to: 1
counter.value++; // Signal value changed to: 2

unsubscribe();
counter.value++; // <- no logs are printed because the listener was removed

Create a dependent Signal

Create a new signal from a list of other signal dependencies using computed(). Always remember to cleanup the computed signal after usage.

import { computed, signal } from "react-signals";

const seconds = signal(150);
const [ minutes, cleanup ] = computed(() => seconds.value / 60, [seconds]);

console.log(minutes.value); // 2.5
seconds.value = 360;
console.log(minutes.value); // 6

cleanup(); // <- cleanup the computed listener when not needed anymore 

Manage side-effects

Run a side-effect immediately and on signal changes using task(). You can return a cleanup function that will run before every side-effect is run. Always remember to cleanup the task after usage.

import { signal, task } from "react-signals";

const user = signal("John");
const cleanup = task(
	() =>
	{
		const value = user.value;
		console.log("Connected as user " + value);
		return () => console.log("Disconnected user " + value);
	},
	[user],
); // Connected as user John

user.value = "Paul"; // Disconnected user John
                     // Connected user Paul

cleanup(); // Disconnected user Paul

Manage asynchronous data

Create an asynchronous signal using resource(). You can refetch the data signal and find the current status.

The resource data fetch will start only when refetch() is called or the data field is accessed.

Accessing the data signal's value will throw a Promise when first loading the data. This behaviour makes the resource work with React.Suspence by default.

Accessing the data signal's value will re-throw any value thrown by the fetch function.

import { resource } from "react-signals";

const [ username, cleanup ] = resource(() => Promise.resolve("John"));

console.log(username.status); // pending
username.refetch().then(() => console.log(username.status, username.data.value)); // success John

Includes all the necessary React hooks

Use a signal value in a component

You can directly show a signal value as a component using element.

import { signal } from "react-signals";

const counter = signal(0);

function Counter()
{
	return (
		<p>
			Current value: {counter.element}
		</p>

		<button type="button" onClick={() => { counter.value++; }}>increment</button>
	);
}

You can retrieve the value in your custom component using useSignalValue. The component will be re-rendered every time the signal changes.

import { signal, useSignalValue } from "react-signals";

const counter = signal(0);

function Counter()
{
	const value = useSignalValue(counter);

	return (
		<p>
			Current value: {value}
		</p>

		<button type="button" onClick={() => { counter.value++; }}>increment</button>
	);
}

You can also pass a selector to get the specific field you need. If the retrieved value did not change after a signal update, the component will not re-render.

import { signal, useSignalValue } from "react-signals";

const user = signal({ firstName: "John", lastName: "Doe" });

function Foo(props: {})
{
	const firstName = useSignalValue(user, () => user.value.firstName);

	return (
		<p>
			First Name: {firstName}
		</p>
	);
}

Create stable Signal references during render

Create signals, computed signals, tasks or resources using the useSignal, useComputer, useTask and useResource hooks during render.

Every reference will be stable and will not change between renders.

import { useSignal, useSignalValue } from "react-signals";

function Counter()
{
	const counter = useSignal(0);

	return (
		<p>
			Current value: {counter.element}
		</p>

		<button type="button" onClick={() => { counter.value++; }}>increment</button>
	);
}