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

v0.0.1-alpha.0

Published

A lightweight and modular state management library for React applications, designed to handle various state scenarios such as **basic**, **asynchronous**, **loadable**, and **controllable** states. With support for **nested states** and seamless integrati

Downloads

16

Readme

react-state-forge

A lightweight and modular state management library for React applications, designed to handle various state scenarios such as basic, asynchronous, loadable, and controllable states. With support for nested states and seamless integration with React Suspense, this library ensures efficient state management by minimizing unnecessary rerenders.

This library covers a broad range of state needs, from simple value storage to asynchronous data management and polling-based controls:

  • Loadable State: Suitable for on-demand data fetching (e.g., API requests).
  • Controllable Loadable State: Ideal for managing polling, WebSocket connections, or Server-Sent Events (SSE), giving you the ability to pause, resume, or reset the loading process.

Whether you need simple state handling, asynchronous logic, or precise control over complex loading flows, this library provides the flexibility to fit your needs.

Installation

using npm:

npm install --save react-state-forge

or yarn:

yarn add react-state-forge

or pnpm:

pnpm add react-state-forge

API

| | State | AsyncState | LoadableState | ControllableLoadableState | | ------------------------------------------------------------------------------------------------------------------------------------- | :-------------: | :-----------------------: | :-----------------------------: | :-----------------------------------------------------: | | getValue | ✅ | ✅ | ✅ | ✅ | | setValue | ✅ | ✅ | ✅ | ✅ | | onValueChange | ✅ | ✅ | ✅ | ✅ | | useValue / Controller | ✅ | ✅ | ✅ | ✅ | | useMappedValue / MappedController | ✅ | ✅ | ✅ | ✅ | | useMergedValue / MergedController | ✅ | ✅ | ✅ | ✅ | | use / SuspenseController | ❌ | ✅ | ✅ | ✅ | | useAll / SuspenseAllController | ❌ | ✅ | ✅ | ✅ | | getPromise | ❌ | ✅ | ✅ | ✅ | | onSlowLoading | ❌ | ✅ | ✅ | ✅ | | awaitOnly / SuspenseOnlyController / SuspenseOnlyAllController | ❌ | ✅ | ✅ | ✅ | | .error | ❌ | ✅ | ✅ | ✅ | | .isLoaded | ❌ | ✅ | ✅ | ✅ | | .load | ❌ | ❌ | ✅ | ✅ | | withoutLoading | ❌ | ❌ | ✅ | ✅ | | .loading.pause | ❌ | ❌ | ❌ | ✅ | | .loading.resume | ❌ | ❌ | ❌ | ✅ | | .loading.reset | ❌ | ❌ | ❌ | ✅ |

createState

createState<T>(): State<T | undefined>;

createState<T>(initialValue: T): State<T>;

createState<T>(getInitialValue: () => T): State<T>;

Creates a basic state with an initial value.

import createState from 'react-state-forge/createState';
import useValue from 'react-state-forge/useValue';
import setValue from 'react-state-forge/setValue';

const togglerState = createState(false);

const Togglers = () => (
  <div>
    <button
      onClick={() => {
        setValue(togglerState, true);
      }}
    >
      turn on
    </button>
    <button
      onClick={() => {
        setValue(togglerState, false);
      }}
    >
      turn off
    </button>
    <button
      onClick={() => {
        setValue(togglerState, (prevValue) => !prevValue);
      }}
    >
      switch
    </button>
  </div>
);

const Light = () => {
  const value = useValue(togglerState);

  return <div>light {value ? 'on' : 'off'}</div>;
};

const Component = () => (
  <div>
    <div>
      <Light />
    </div>
    <div>
      <Togglers />
    </div>
  </div>
);

createNestedState

createNestedState<T>(): NestedState<T | undefined>;

createNestedState<T>(initialValue: T): NestedState<T>;

createNestedState<T>(getInitialValue: () => T): NestedState<T>;

Creates a nested state, where individual parts behave independently, triggering updates only when specific parts change. It allows for more efficient reactivity in complex data structures by minimizing unnecessary rerenders.

import createNestedState from 'react-state-forge/createNestedState';
import useValue from 'react-state-forge/useValue';
import setValue from 'react-state-forge/setValue';

const userState = createNestedState({
  profile: { name: 'Alice', age: 25 },
});

const Name = () => {
  const nameState = userState.scope().profile.name.end$;

  const name = useValue(nameState);

  return (
    <input
      value={name}
      onChange={(e) => {
        setValue(nameState, e.target.value);
      }}
    />
  );
};

const Age = () => {
  const ageState = userState.scope().profile.age.end$;

  const age = useValue(ageState);

  return (
    <input
      value={age}
      type='number'
      onChange={(e) => {
        setValue(ageState, +e.target.value);
      }}
    />
  );
};

const Component = () => (
  <div>
    <div>
      <Name />
    </div>
    <div>
      <Age />
    </div>
  </div>
);

createAsyncState

Creates a state with varying levels of capabilities based on the provided options:

createAsyncState<T, E = any>(
  options?: AsyncStateOptions<T>
): AsyncState<T, E>;

type AsyncStateOptions<T> = {
  value?: T | (() => T);
  isLoaded?(value: T, prevValue: T | undefined, attempt: number): boolean;
};

represents a state that supports asynchronous operations. It extends a regular state by introducing the following additional internal states:

  • .error: A state that holds the latest error, if one occurred during loading.
  • .isLoaded: A state that indicates whether the state has successfully loaded.
createAsyncState<T, E = any>(
  options: LoadableStateOptions<T, E>
): LoadableState<T, E>;

type LoadableStateOptions<T, E = any> = AsyncStateOptions<T> & {
  load(this: AsyncState<T, E>): void | (() => void);
  loadingTimeout?: number;
  reloadIfStale?: number;
  reloadOnFocus?: number;
};

Extends AsyncState with additional loading functionality, allowing the state to be loaded or reloaded.

Extends LoadableState with additional control over the loading process, including pause, resume, and reset.

createAsyncState<T, E = any>(
  options: ControllableLoadableStateOptions<T, E>
): ControllableLoadableState<T, E>;

type ControllableLoadableStateOptions<T, E = any> = LoadableStateOptions<
  T,
  E
> & {
  pause(): void;
  resume(): void;
  reset(): void;
};

License

MIT © Krombik