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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hook-shared-state

v1.0.8

Published

Dead simple global shared state, using React Hooks.

Downloads

10

Readme

useSharedState

Dead simple global shared state, using React Hooks.

Quick Start

Simple shared state


// updateEmail.tsx
import { useSharedState } from 'use-shared-state';

const UpdateEmail = () => {
  const [email, setEmail] = useSharedState('userEmail');
  
  return <input value={email} onChange={setEmail} />;
}

export default UpdateEmail;

// displayEmail.tsx
import { useSharedState } from 'use-shared-state';

const DisplayEmail = () => {
  const [email] = useSharedState('userEmail');
  
  return <span>User email: {email}</span>;
}

export default DisplayEmail;

Middleware

Middleware is useful for a variety of use cases.

// App.tsx
import { registerMiddleware } from 'use-shared-state';

function transformData(currentValue, next) {
  next(`the current value is ${currentValue}`);
}

registerMiddleware('data', transformData);

// DataDisplay.tsx
const DataDisplay = () => {
  const [current] = useSharedState('data', 'foo');

  return <p>{current}</p>;
}

Update state outside a component

Sometimes it's desirable for external systems to alter app state -- updates from other clients through pub/sub, mqtt, etc, are good examples of this.

useSharedState exposes a global update method that allows for updating a key outside of a component.

// pubSubClient.ts
import { update } from 'use-shared-state';

const client = new PubSubClient();

client.subscribe('email-changes', (email) => {
  update('email', email);
});

Redux-like actions

Redux operates with actions and reducers. useSharedState should handle all of the use cases that Redux does, however it removes the need for decoupled actions and reducers, making your code easier to reason about.

Two patterns present themselves with this library for intercepting and acting on inputs (actions) and transforming the outputs (reducers).

Redux + saga/thunk, etc

You can emulate the async action from Redux popularized by libraries such as saga or thunk using this library, for example:

// Intercept some data with middleware to hydrate an object
import { registerMiddleware } from 'use-shared-state';

function fetchUser(currentValue, next) {
  if (typeof currentValue === 'string') {
    const response = await fetch(`api.example.com/user/${currentValue}`);
    const user = await response.json();

    next(user);
  } else {
    next(currentValue);
  }
}

registerMiddleware(user, fetchUser);

const UserDisplay = ({ userId }) => {
  const [user] = useSharedState('user', userId);

  return typeof user === 'string' ? <p>Loading user...</p> : <p>{user.name}</p>
}

However, a composition pattern is probably clearer and easier to reason about:

// Wrap your function in an HoC for re-use
async function fetchUser(id, update) {
  const response = await fetch(`https://api.example.com/user/${id}`);
  const user = await response.json();

  update(`user-${userId}`, user);
}

function WithUser(Component) {
  return ({ userId }) => {
    const [user, setUser] = useSharedState(`user-${userId}`);

    if (user === undefined) {
      fetchUser(userId, setUser);
    }

    return <Component user={user} />;
  }
}

// elsewhere
const UserDisplay = ({ user }) => {
  return user === undefined ? <p>Loading user...</p> : <p>{user.name}</p>
}

export default WithUser(UserDisplay);

These patterns eliminate the need for boilerplate action management, and keep the fetching of data and the use of the data more closely coupled in your code, making it easier to trace the data flow in your app.