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

x-state-manager

v1.0.2

Published

A lightweight state manager built on top of SWR for efficient state management with caching, optimistic updates, and modular architecture. It allows easy global and local state management, automatic data re-fetching on focus, and supports TypeScript

Downloads

163

Readme

X-STATE-MANAGER

A lightweight state manager built on top of SWR for efficient state management with caching, optimistic updates, and modular architecture. It allows easy global and local state management and supports TypeScript for enhanced type-safety.

Installation

NPM

npm i x-state-manager

YARN

yarn add x-state-manager

Usage example

store/counterStoreData.ts

// store key for managing counter state
export const COUNTER_STORE_DATA_KEY = 'COUNTER_STORE_DATA_KEY';

export type CounterState = number;

// fetch counter from local storage. Here can be any API call
export const fetchCounter = async (): Promise<CounterState> => {
  const value = localStorage.getItem('counter');
  return value ? Number(value) : 0;
};

store/useCounter.ts

import { createSWRState } from 'x-state-manager';
import { COUNTER_STORE_DATA_KEY, fetchCounter } from './counterStoreData';

// create and return counter state
export const useCounter = () => {
  // COUNTER_STORE_DATA_KEY is the key for managing counter state (it should be unique for each state)

  // fetchCounter is the function for fetching counter from local storage or API (here can be any API call or null)

  // Third argument is the options (it is optional) for SWR (https://swr.vercel.app/docs/api#options)
  // {
  //   fallbackData: 0, --> default state value
  // }

  return createSWRState<number>(COUNTER_STORE_DATA_KEY, fetchCounter, {
    fallbackData: 0,
  });
};

Counter.tsx

'use client';

import { useCounter } from '../store/useCounter';

const Counter: React.FC = () => {
  const { data: count, mutate, isLoading } = useCounter();

  const increment = () => {
    // first argument can be new value or function with previous value that will be return new value.
    // second argument is boolean to determine if should revalidate the data
    mutate((prevData) => (prevData ?? 0) + 1, false);
  };

  return (
    <div className='flex flex-col gap-8 '>
      <h1>Count: {isLoading ? 'Loading...' : count}</h1>

      <button onClick={increment} className='rounded bg-blue-500 px-4 py-2'>
        Increment
      </button>
    </div>
  );
};

export default Counter;

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.