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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gks101/localyx

v1.0.2

Published

A React hook for robust local storage state management with TTL and cross-tab sync.

Readme

@gks101/localyx

A lightweight (<1kb gzip+minified )React hook for persistent localStorage state with TTL expiry, same-tab and cross-tab sync, and optional encoding/serialization controls.

Installation

npm install @gks101/localyx

Quick Start

import { useLocalStorageState } from '@gks101/localyx';

function ThemeToggle() {
  const [theme, setTheme, clearTheme] = useLocalStorageState<'light' | 'dark'>('theme', 'dark');

  return (
    <div>
      <p>Theme: {theme}</p>
      <button onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>Toggle</button>
      <button onClick={clearTheme}>Reset</button>
    </div>
  );
}

Features

  • SSR-safe access guards.
  • Same-tab and cross-tab synchronization.
  • TTL expiry with automatic in-tab cleanup.
  • Expiry strategies: absolute and sliding.
  • onExpire lifecycle callback.
  • Optional namespaced keys.
  • Custom serializer and encrypt/decrypt functions.
  • Utility: getRemainingTtl(...).

TTL Example (absolute + callback)

import { useLocalStorageState } from '@gks101/localyx';

function Session() {
  const [token, setToken, clearToken] = useLocalStorageState<string | null>('session', null, {
    ttl: 30 * 60 * 1000, // 30m
    ttlStrategy: 'absolute',
    onExpire: ({ key }) => {
      console.log(`Expired key: ${key}`);
    },
  });

  return (
    <div>
      <p>{token ?? 'No active token'}</p>
      <button onClick={() => setToken(`token_${Date.now()}`)}>Login</button>
      <button onClick={clearToken}>Logout</button>
    </div>
  );
}

Sliding TTL Example

const [cache] = useLocalStorageState('search_cache', '', {
  ttl: 5 * 60 * 1000,
  ttlStrategy: 'sliding',
});

sliding refreshes the timestamp on read/sync access so active values remain alive.

Namespaced Keys

const [user] = useLocalStorageState('profile', null, {
  namespace: 'app:v1',
});

This stores under app:v1:profile.

Remaining TTL Utility

import { getRemainingTtl } from '@gks101/localyx';

const remainingMs = getRemainingTtl('session', 30 * 60 * 1000, {
  namespace: 'app:v1',
});

Returns:

  • number (ms remaining),
  • 0 when expired-but-not-yet-cleaned,
  • null when key is missing, not timestamped, or invalid.

Encryption / Serialization Notes

  • Default behavior applies Base64 obfuscation.
  • Use encrypt: null and decrypt: null to store plain JSON.
  • Base64 is not cryptographic encryption; provide custom crypto functions for real security.

API

useLocalStorageState<T>(key, initialValue, options?)

Returns [state, setState, removeValue].

options:

  • ttl?: number
  • ttlStrategy?: 'absolute' | 'sliding' (default: 'absolute')
  • namespace?: string
  • onExpire?: (ctx: { key: string; value: T }) => void
  • now?: () => number
  • encrypt?: ((raw: string) => string) | null
  • decrypt?: ((raw: string) => string) | null
  • serializer?: { stringify: (v: T) => string; parse: (s: string) => T }

getRemainingTtl(key, ttl, options?)

options:

  • namespace?: string
  • decrypt?: ((raw: string) => string) | null
  • parse?: (s: string) => { v: unknown; t?: number }
  • now?: () => number

Production Patterns

  • Auth/session token cache with hard expiry.
  • Feature flag payload cache with short TTL.
  • Local stale-while-revalidate snapshot cache.

Troubleshooting

  • Value not syncing in same tab:
    • ensure both hooks use the same key and namespace.
  • Value not expiring:
    • confirm ttl is set and data was written by this hook.
  • Unexpected reset:
    • invalid/corrupted payloads are wiped by design.

License

MIT