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

staterino

v2.0.0

Published

hook based state management

Downloads

20

Readme

Staterino npm size

Simple hook based state management.

Example

import { render } from 'preact'
import * as hooks from 'preact/hooks'
import merge from 'mergerino'
import staterino from 'staterino'

const state = { count: 0 }

const useStore = staterino({ merge, hooks, state })

const { set, get, subscribe } = useStore
const increment = () => set({ count: x => x + 1 })
const decrement = () => set({ count: x => x - 1 })

// reset count when it reaches 11
subscribe(
  s => s.count,
  count => {
    if (Math.abs(count) > 10) set({ count: 0 })
  }
)

const App = () => {
  const count = useStore(s => s.count)
  return (
    <div>
      <p>Count is {count}</p>
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
    </div>
  )
}

render(<App />, document.getElementById('app'))

Code sandbox

Usage

Staterino exports a single function: staterino.

This function creates a staterino data store, it expects a single object of the following shape as its only parameter:

const useStore = staterino({
  // initial state object
  state,
  // reducer function that combines current state with a patch
  merge,
  // staterino relies on these two hooks to function
  hooks: { useLayoutEffect, useReducer }
})

You can create as many data stores as you like, each holding their own isolated state.

useStore accepts one parameter, a state selector.

A state selector can be either a function:

const count = useStore(state => state.counter.count)

a string:

const count = useStore('counter.count')

or an array of strings/functions:

const [count, age] = useStore(['counter.count', state => state.age])

If you pass an array the hook will return an array as well with the state slices in the correct order.

If no arguments are passed useStore() will return the whole state object:

const state = useStore()

useStore is the hook itself, but it contains 3 essential functions:

const {
  // sends a patch to be merged with the current state
  set,
  // getter for current state
  get,
  // allows you to react to state changes outside of components
  subscribe
} = useStore

subscribe takes two parameters, a state selector or array of state selectors, and a callback for when the subscribed portion of state changes:

// the subscribe call returns a function used to unsubscribe
const unSub = subscribe(
  // the state selector
  ['counter.count', state => state.age],
  // the callback function that triggers when state changes
  (count, age) => {
    console.log(count, age)
    // optional cleanup function, similar to useEffect
    return () => console.log('cleaning up', count, age)
  }
)

The selector parameter works under the same rules as the one passed to useStore, it can be a string a function or an array with a mix of the two.

If you just want to subscribe to any change to the state overall you can just pass a single parameter as a shorthand:

subscribe(state => {
  // do something whenever state changes
})

Credits

Inspired by zustand ❤️