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

watchi-store

v0.1.9

Published

Small, controllable and performant state management for React

Downloads

89

Readme

Watchi-store is a simple typescript compatible state management library for React, focused on controllability and performance.

Features

  • Tiny package (1.9kB minified)
  • Highly controllable
  • Easily create multiple stores
  • Update store from anywhere, no dispatches, no reducers, just change values
  • Automatically update your UI, simply watch for values in the store
  • Use the props pattern with values from the store

It's inspired by libraries like Redux, Zustand and MobX. The main difference between these libraries and Watchi-store is that this library does not enforce immutability in the store, changes anywhere in the store will cause watchers (listeners) and corresponding UI to be automatically updated. This can be useful when working with a large amount of deeply nested state that you want to control outside of the individual UI components, for example from actions.

This library will most likely not offer any benefits if you are not working with large amounts of nested states or need improved state performance.

Usage

Create a store

import Store from 'watchi-store'

type MyStore = { a: { [key: string]: { name: string; n: number } } }
const myStore = new Store<MyStore>({
  a: {
    b1: {
      name: 'my name'
      n: 0,
    }, 
    b2: {
      name: 'my name'
      n: 1,
    }
  }
})

Watch store values

function MyComponent() {
  const b2n = myStore.useWatch(store => store.a.b2.n)

  return <div onClick={() => myStore.store.a.b2.n++}>b2n: {b2n}</div>
}

Watch store values based on props (don't search through store)

function A(){
  const a = useWatch(store => store.a)

  return <>
    <B b={a.b1} />
    <B b={a.b2} />
  </>
}

function B(props: { b: { n: number }, increment: () => void }) {
  // watch value from props, same reference as from store
  const n = useWatch(() => props.b.n)
  
  return <button onClick={() => props.b.n++}>{props.b.name}: {n}</button>
}

Update the store from anywhere

const time = new Store({ passed: 0 })
setTimeout(() => time.store.passed++, 1000)

function Passed() {
    const passed = time.useWatch(store => store.passed)

    return <div>{passed} has passed</div>
}

Additional utilities

useRefWatch(): Watch store without rerendering UI on changes watch(): Call callback whenever the store updates revertable(): Perform a revertable action on the store revertableGlobal(): Like revertable but any changes made during the action will can be reverted, including changes outside of the action transaction(): Perform a transaction on the store, changes to the store will only be applied when the transactions finishes successfully revertOnError(): Perform a revertable action on the store, actions are reverted when an error is caught revertOnErrorGlobal(): Like revertOnError but reverts all changes made during the action, including changes outside of the action target(): Returns the original unwatched object of a watched object