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

signals-react

v1.0.1

Published

signals for react

Downloads

26

Readme

signals-react

signals-react is a React hook library designed to offer a reactive state management solution inspired by signal mechanisms, but tailored specifically for React applications. By leveraging the rxjs observables, it introduces a robust and adaptable approach to managing state changes within components. This library enables developers to create signals, compute derived signals, and manage side effects seamlessly.

Example with global state

Example with local state

Installation

To integrate signals-react into your project, install it through npm by executing:

npm install signals-react

Usage

Begin by importing createSignal from signals-react to initiate signal creation and management in your React app.

Creating a Signal

import { createSignal } from 'signals-react';

const countSignal = createSignal(0); 
// Initializes a global signal with an initial value of 0.

const CounterComponent = () => {
  const value = countSignal.useValue(); 
  // Subscribes to signal changes, rerendering the component accordingly.

  return (
    <div>
      <p>Current count: {value}</p>
      <button onClick={() => {
        countSignal.setValue((prev) => prev + 1)
      }}>Increment</button>
    </div>
  );
};

Rendering Optimizations

Utilize the signal directly within JSX to minimize component subscriptions and unnecessary re-renders. Only the signal's content will be updated:

const countSignal = createSignal(0);

function Unoptimized() {
  // Triggers component re-renders on countSignal changes.
  const value = countSignal.useValue();
  return <p>{value}</p>;
}

function Optimized() {
  // Updates text automatically without rerendering the component.
  return <p>{countSignal}</p>;
}

Local State Management with Signals

While most application states are managed globally, components often require their own internal state. For such cases, signals-react offers useSignal and useComputed hooks for creating and computing signals within components:

import { useSignal } from "signals-react";

function Counter() {
  const count = useSignal(0);
  const double = count.useComputed(value => value * 2);

  return (
    <div>
      <p>Current count: {count}, double: {double}</p>
      <button onClick={() => count.setValue(count.peek() + 1)}>Increment</button>
    </div>
  );
}

Advanced Features

Using peek()

The peek() method enables reading the current signal value without subscription or triggering re-renders, ideal for event-based updates:

function Counter() {
  const count = useSignal(0);
  return (
    <div>
      <button onClick={() => count.setValue(count.peek() + 1)}>Increment</button>
    </div>
  );
}

Using compute

The compute method facilitates the creation of ComputedSignals from existing signals, allowing for dynamic and reactive derived states:

const celsiusSignal = createSignal(0);
const fahrenheitSignal = celsiusSignal.compute(c => c * 9 / 5 + 32);

celsiusSignal.setValue(36); // Automatically updates fahrenheitSignal.

const TemperatureConverter = () => {
  return (
    <div>
      <p>Celsius: {celsiusSignal.useValue()}</p>
      <p>Fahrenheit: {fahrenheitSignal.useValue()}</p>
    </div>
  );
};

This method enhances state management by enabling efficient and logical reactive transformations, streamlining the development of complex reactive systems.

License

signals-react is distributed under the MIT License. For more details, refer to the LICENSE file in the repository.