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

nextjs-state

v0.1.2

Published

Type-safe state management for Next.js applications

Readme

nextjs-state

License Types npm npm bundle size GitHub issues GitHub stars

Features

  • 🎯 Type-Safe: Full TypeScript support with zero configuration
  • 🚀 Simple API: Intuitive hooks-based API
  • Lightweight: ~4KB minified + gzipped
  • 💾 Built-in Persistence: Optional localStorage persistence
  • 🔄 Middleware System: Extensible with custom middleware
  • 🛠️ Developer Tools: Time-travel debugging (coming soon)

Installation

npm install nextjs-state
# or
yarn add nextjs-state
# or
pnpm add nextjs-state

Quick Start

// state/index.ts
import { createNextState } from 'nextjs-state';

interface AppState {
  count: number;
  lastUpdated: string;
}

export const { Provider, useNextState } = createNextState<AppState>({
  initialState: {
    count: 0,
    lastUpdated: new Date().toISOString(),
  },
});

// app/layout.tsx
import { Provider } from './state';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html>
      <body>
        <Provider>{children}</Provider>
      </body>
    </html>
  );
}

// app/components/Counter.tsx
import { useNextState } from '../state';

export function Counter() {
  const { state, setState } = useNextState((state) => ({
    count: state.count,
    lastUpdated: state.lastUpdated,
  }));

  const increment = () => {
    setState({
      count: state.count + 1,
      lastUpdated: new Date().toISOString(),
    });
  };

  return (
    <div>
      <p>Count: {state.count}</p>
      <p>Last Updated: {new Date(state.lastUpdated).toLocaleString()}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

Core Concepts

State Management

nextjs-state uses a simple and intuitive state management pattern:

  1. Define your state with TypeScript interfaces
  2. Create a state instance with createNextState
  3. Wrap your app with the Provider
  4. Use useNextState hook to access and update state

Type Safety

interface User {
  id: string;
  name: string;
  email: string;
}

interface AppState {
  user: User | null;
  theme: 'light' | 'dark';
}

// Type errors caught at compile time
const wrongUpdate = setState({
  user: {
    id: 123, // Error: Type 'number' is not assignable to type 'string'
    name: 'John', // Error: Missing property 'email'
  },
});

State Updates

// Simple update
setState({ count: count + 1 });

// Update with previous state
setState((prev) => ({
  count: prev.count + 1,
}));

// Async update
const fetchUser = async (userId: string) => {
  const user = await api.getUser(userId);
  setState({ user });
};

Middleware

const loggerMiddleware = {
  id: 'logger',
  onStateChange: (prev, next) => {
    console.log('State updated:', { prev, next });
  },
  onError: (error) => {
    console.error('State error:', error);
  },
};

const { Provider } = createNextState({
  initialState,
  options: {
    middleware: [loggerMiddleware],
  },
});

Persistence

State is automatically persisted to localStorage by default. You can access the storage API directly:

const { Provider, useNextState } = createNextState({
  initialState: {
    count: 0,
  },
});

// State will be automatically persisted to localStorage

API Reference

createNextState<T>

Creates a new state instance with the provided configuration.

const { Provider, useNextState } = createNextState<AppState>({
  initialState: {
    // Your initial state
  },
  options?: {
    middleware?: NextStateMiddleware<T>[];
  },
});

useNextState

Hook to access and update state.

const { state, setState } = useNextState((state) => ({
  // Select state properties
}));

NextStateMiddleware

Interface for creating custom middleware.

interface NextStateMiddleware<T> {
  id?: string;
  priority?: number;
  onStateChange?: (prev: T, next: T) => void | Promise<void>;
  onError?: (error: Error) => void;
}

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT © [Rahees Ahmed]