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

react-sweet-context

v2.0.6

Published

A lightweight React store implementation

Readme

React SweetContext

A lightweight, performant state management solution for React applications built with Bun and TypeScript.

Overview

React SweetContext provides an efficient, minimal implementation of the store pattern for React applications. Designed to be lightweight and performant, it offers:

  • Immutable state management with automatic equality checking
  • Context-based store access for components
  • Automatic subscription handling with useSyncExternalStore
  • Action creation support with typed selectors
  • Shallow equality checking for optimized re-renders
  • Zero dependencies - built with React and TypeScript only

Quick Start

Creating a Store Context

import { createSweetContext, createHook } from "react-sweet-context";

// Define your state type
type User = {
  id: number;
  name: string;
  email: string;
};

// Define initial state
const initialState: User = {
  id: 0,
  name: "guest",
  email: "",
};

// Create the context
const userContext = createSweetContext({
  name: "UserContext", // Optional name for debugging
  initState: initialState,
  action: ({ set }) => ({
    updateName: (name: string) => {
      set({ name });
    },
    updateEmail: (email: string) => {
      set({ email });
    },
    resetUser: () => {
      set(initialState);
    },
  }),
});

// Create the hook for component usage
export const useUser = createHook(userContext);

Using in Components

import { useUser } from "./stores/UserStore";

function UserProfile() {
  const [user, actions] = useUser();

  return (
    <div>
      <p>Name: {user.name}</p>
      <p>Email: {user.email}</p>
      <button onClick={() => actions.updateName("New Name")}>
        Update Name
      </button>
      <button onClick={() => actions.updateEmail("[email protected]")}>
        Update Email
      </button>
    </div>
  );
}

API Reference

createSweetContext(props)

Create a lightweight store instance that is the default value of the context; each time a Container is rendered, a new store instance will be created.

Parameters:

  • props - Configuration object with:
    • name?: Optional name for debugging
    • initState: Initial state value to be used for the store
    • action: Action creator function that returns action methods for the store

createHook(context, selector)

Creates a React hook that provides access to store state and actions.

Parameters:

  • context: The store context created by createSweetContext
  • selector?: Optional function to extract specific values from state

createAction(context, selector)

Creates a React hook that provides access to store actions without subscribing to state changes.

Parameters:

  • context: The context created by createSweetContext
  • selector?: Optional function to extract specific action methods from the store

createConsumer(context, selector)

Creates a React Consumer component that provides access to store state and actions.

Parameters:

  • context: The context created by createSweetContext
  • selector?: Optional function to extract specific values from state

createContainer(context, config)

Creates a React component that provides access to store state and actions.

Parameters:

  • context: The context created by createSweetContext
  • config?: Optional configuration object for container lifecycle events

Configuration Object:

The config parameter allows you to hook into the container's lifecycle events:

  • onInit?(api, action, props): Called when the container is initialized. Receives:

    • api: The store API for state manipulation
    • action: The action methods for the store
    • props: The container's props
  • onUpdate?(api, action, props, prev): Called when the container's props are updated. Receives:

    • api: The store API for state manipulation
    • action: The action methods for the store
    • props: The new props
    • prev: The previous props

Returns:

  • A React component that wraps children with store provider

Installation

bun install react-sweet-context

Performance Considerations

Shallow Equality Checking

React SweetContext uses shallow equality checking to optimize re-renders:

// The following will only trigger a re-render if the reference changes
const useUser = createHook(context);

Custom Selectors

Use custom selectors to extract only necessary data:

// Only re-renders when count changes, not the entire state object
const useCount = createHook(context, (state) => state.count);