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-listenerstore

v1.1.19

Published

A lightweight global state management solution for React applications with support for nested state and listeners.

Downloads

434

Readme

react-listenerstore

A lightweight global state management solution for React applications with support for nested state and listeners.

Features

  • Nested state management: Manage deeply nested state structures.
  • Global state store: Create and access multiple named stores globally.
  • Listeners: Register listeners to state changes at any level.
  • React Hooks: Integrate seamlessly with React using hooks.

Installation

To install the module, you can use npm or yarn:

npm install react-listenerstore
# or
yarn add react-listenerstore

Usage

  1. Creating a Store - To create a store, use the `createListenerStore` function. Provide a unique namespace and the initial state.
// store.ts
import { createListenerStore } from 'react-listenerstore';

const { useListener, setListenerStore } = createListenerStore('myNamespace', {
  a: 1,
  b: {
    c: 2,
    d: 3
  }
});
  1. Using the Store in a Component - Use the `useListener` hook to access and update the store within your components.
import React from 'react';
import { useListener } from './store.ts';

const MyComponent = () => {
  const [ aValue, setAValue ] = useListener('a');
  const [ bCValue, setBCValue ] = useListener('b.c');

  return (
    <div>
      <div>
        A Value: {aValue}
        <button onClick={() => setAValue((prev) => prev + 1)}>Increment A</button>
      </div>
      <div>
        B.C Value: {bCValue}
        <button onClick={() => setBCValue(bCValue + 1)}>Increment B.C</button>
      </div>
    </div>
  );
};

export default MyComponent;
  1. Updating the Store Outside of Components - Use the setListenerStore function to update the store outside of a component.
import { setListenerStore } from './store.ts';

// Update store directly
setListenerStore('a', 10);

// Update store using a function
setListenerStore('b.c', (currentValue) => currentValue + 5);

API

createListenerStore(namespace: string, initialState: T)

Creates a new global store.

Parameters namespace (string): A unique name for the store. initialState (T): The initial state object. Returns useListener: A hook to access and update the store within components. setListenerStore: A function to update the store outside of components.

useListener(key: K)

A React hook to access and update the store.

Parameters key (K): The key to access in the store. Supports nested keys (e.g., a, b.c). Returns data: The current value at the specified key. set: A function to update the value at the specified key. set(data: P | ((data: P) => P)): Updates the key's value in the store.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contribution

Feel free to open issues or submit pull requests if you have any improvements or bug fixes.

Acknowledgements

Thanks to the open-source community for continuous inspiration and support.