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

quhold

v1.0.1

Published

QuHOLD is a simple state container library for React applications, providing an easy way to manage and synchronize state across components.

Downloads

9

Readme

QuHOLD - Simplified State Container for React App

QuHOLD is a simple state container library for React applications, providing an easy way to manage and synchronize state across components.

Key Features

  1. Simple API: Easy to use methods for setting and getting state.

  2. Subscription Management: Allows components to subscribe to state changes and automatically updates them.

  3. Type Safety: Written in TypeScript to provide type safety and better developer experience.

  4. Global State Management: Easily manage global state across your application.

  5. React Integration: Custom hook for seamless integration with React components.

Installation

To install QuHOLD, you can simply add it to your project:

npm install quhold

Then, import the necessary functions and create your state management effortlessly!

Usage Example

Basic State Management

import React from "react";
import { createHold, useHold } from "quhold";

const counterHold = createHold(0);

function Counter() {
  const [count, setCount] = useHold(counterHold);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
}

function App() {
  return (
    <div>
      <h1>Counter Example</h1>
      <Counter />
      <Counter />
    </div>
  );
}

export default App;

In this example:

  • We create a Hold instance counterHold with an initial value of 0.
  • We use the useHold hook to bind the counterHold to the Counter component.
  • The Counter component displays the current count and provides buttons to increment and decrement the count.
  • The App component renders two Counter components, both of which share the same state.

Global State Management

To use QuHOLD for global state management, follow these steps:

  1. Create a Hold instance for your global state:
// state.js
import { createHold } from "quhold";

export const globalCounterHold = createHold(0);
  1. Use the Hold instance in your components:
// Counter.js
import React from "react";
import { useHold } from "quhold";
import { globalCounterHold } from "./state";

function Counter() {
  const [count, setCount] = useHold(globalCounterHold);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
}

export default Counter;
  1. Render the components in your application:
// App.js
import React from "react";
import Counter from "./Counter";

function App() {
  return (
    <div>
      <h1>Global Counter Example</h1>
      <Counter />
      <Counter />
    </div>
  );
}

export default App;

In this example:

  • We create a Hold instance globalCounterHold in state.js with an initial value of 0.
  • We use the useHold hook to bind the globalCounterHold to the Counter component.
  • The Counter component displays the current count and provides buttons to increment and decrement the count.
  • The App component renders two Counter components, both of which share the same global state.

Notes

  • The Hold class uses a set to manage subscribers, ensuring that each subscriber is unique.
  • The useHold hook handles the subscription and cleanup automatically, making it easy to integrate with React components.

API Documentation

Hold<T>

A class that holds a value of type T and allows subscribers to listen for changes to that value.

Constructor

new Hold<T>(initialValue: T)

initialValue (T) The initial value of the state.

Methods

  • getValue(): T Returns the current value of the state.
  • setValue(newValue: T): void Sets a new value to the state and notifies all subscribers if the new value is different from the current value.
  • subscribe(subscriber: Subscriber<T>): () => void Adds a subscriber function that will be called whenever the state changes. Returns a function to unsubscribe the subscriber.

createHold<T>(initialValue: T): Hold<T>

A factory function to create a new Hold instance.

  • initialValue (T): The initial value of the state.
  • Returns a Hold<T> instance.

useHold<T>(hold: Hold<T>): [T, (newValue: T) => void]

A custom hook to use a Hold instance in a React component.

  • hold (Hold<T>): The Hold instance to be used.

Returns a tuple:

  • The current value of the state.
  • A function to set a new value to the state.

Contributing

QuHOLD is an open-source project, and contributions are welcome! If you encounter any issues, have feature requests, or want to contribute improvements. To contribute, please follow the guidelines in CONTRIBUTING.

License

QuHOLD is licensed under the MIT License, allowing for both personal and commercial use with attribution. - see the LICENSE file for details.