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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@duckness/store

v1.0.2

Published

Simple store for React components

Downloads

220

Readme

@duckness/store

Simple store for React components. Intended to store data that should not be stored in global store (i.e. redux) or do not need reducers/sagas.

NPM License Libraries.io dependency status for latest release, scoped npm package GitHub issues vulnerabilities npm bundle size

Example

import createStore from '@duckness/store'
import React from 'react'

// Create a store with initial state and actions
const counterStore = createStore({
  initState: { count: 0 },
  actions: {
    increment: (amount = 1) => state => ({
      ...state,
      count: state.count + amount
    }),
    
    decrement: (amount = 1) => state => ({
      ...state,
      count: state.count - amount
    }),
    
    reset: () => state => ({
      ...state,
      count: 0
    })
  }
})

// Usage with hooks in components
function CounterComponent() {
  // Select just what you need from the store
  const count = counterStore.useStore({
    selector: state => state.count
  })
  
  return (
    <div>
      <h2>Counter: {count}</h2>
      <button onClick={() => counterStore.actions.increment()}>+1</button>
      <button onClick={() => counterStore.actions.increment(5)}>+5</button>
      <button onClick={() => counterStore.actions.decrement()}>-1</button>
      <button onClick={() => counterStore.actions.reset()}>Reset</button>
    </div>
  )
}

// Or use Consumer component
function CounterWithConsumer() {
  return (
    <counterStore.Consumer selector={state => state.count}>
      {count => (
        <div>
          <h2>Counter: {count}</h2>
          <button onClick={() => counterStore.actions.increment()}>+1</button>
          <button onClick={() => counterStore.actions.decrement()}>-1</button>
        </div>
      )}
    </counterStore.Consumer>
  )
}

// Direct store manipulation
function incrementAsync() {
  setTimeout(() => {
    counterStore.updateStore(state => ({
      ...state,
      count: state.count + 1
    }))
  }, 1000)
}

Table of Contents

API

Creating a store

Creates a new store with optional initial state and actions:

const store = createStore({
  initState: {}, // optional initial state, defaults to {}
  actions: {}    // optional action creators, defaults to {}
})

Store interface

The store object provides the following methods:

  • useStore(options): React hook to use store state in components
  • Consumer: React component for consuming store state
  • actions: Object containing bound action creators
  • updateStore(updater): Update store state using an updater function
  • getState(selector): Get current state or part of it using a selector
  • subscribe(listener): Subscribe to store changes
  • destroy(): Clear all listeners from the store

useStore hook

const value = store.useStore({
  updateOnMount: (state) => state,              // Optional updater to run when component mounts
  updateOnUnmount: (state) => state,            // Optional updater to run when component unmounts
  selector: (state) => value,                   // Function to select part of the store state
  shouldSelect: function(prevState, nextState), // Control when selector runs based on store state changes
  shouldUpdate: function(prevValue, nextValue), // Control when component updates based on selected value changes (default: whenChanged)
  debounce: milliseconds,                       // Debounce updates by specified milliseconds (default: no debounce)
  async: boolean                                // Process updates asynchronously (default: false)
})

Example:

const username = userStore.useStore({
  selector: state => state.user.name,
  debounce: 300 // only update component 300ms after last state change
})

Consumer component

Same props as in useStore

<store.Consumer
  selector={state => state.user}
  debounce={300}
  async={false}
>
  {user => (
    <div>Hello, {user.name}!</div>
  )}
</store.Consumer>

Selectors

Selectors extract parts of the store state:

// Get the full state
const state = store.getState()

// Use selector function
const username = store.getState(state => state.user?.name)

Exported Helper functions

  • selectAll(value): Returns the entire value (identity function)
  • always(): Always returns true
  • whenChanged(a, b): Returns true if a !== b

@Duckness packages: