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

@nebulus/react-state

v0.0.1

Published

A lightweight and flexible shared state management library for React applications

Downloads

4

Readme

React State

@nebulus/react-state is a lightweight and flexible shared state management library for React applications like redux, zustand and recoil etc. It provides a simple and intuitive API to manage the state of your components, along with middleware support for handling state transformations.

Installation

npm install @nebulus/react-state

Getting Started

Creating State

To create a state using @nebulus/react-state, you can use the createState function. It takes an initial value and optional middleware(s) as parameters. In the example below, we create a useScore hook.

import { createState } from "@nebulus/react-state";

const useScore = createState(0);

Using State in Components

To use the state within your components, you can destructure the result of the state hook just like react useState hook:

function ViewScore() {
  const [score] = useScore();
  return <div>{score}</div>;
}

function UpdateScore() {
  const [, setScore] = useScore();
  return (
    <div>
      <button onClick={() => setScore((x) => x + 1)}>+</button>
    </div>
  );
}

export default function HelloPage() {
  return (
    <div>
      <ViewScore />
      <UpdateScore />
      <h2>Hello world!!!</h2>
    </div>
  );
}

Dispatching State Updates

State updates are done using the dispatch function without react component provided by the created state. In the example, a score is incremented every second using setInterval:

setInterval(() => {
  try {
    useScore.dispatch((x) => x + 1);
  } catch (err) {}
}, 1000);

Get state

Getting state using the getState function without using react component

const state = useScore.getState(); // state will be a number

Listening on change

Adding Listener

const unbind = useScore.onChange((x) => {
  console.log(`changing score`, x);
  // detach event
  unbind();
});

Middleware

Middleware functions allow you to modify or validate the state before it is updated. In the provided example, a validation middleware is defined to enforce a value limit for the useScore state.

import { createState, type Middleware } from "react-state";

function validation(max: number): Middleware<number> {
  function handleChange(val: number) {
    if (val > max) {
      throw new Error("value limit exceeded");
    }
    return val;
  }
  return function validate(initial, onChange) {
    onChange(handleChange);
    return initial;
  };
}

const useScore = createState(0, validation(10));