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

react-shared-store

v0.1.34

Published

State sharing with multiple components and tabs

Downloads

12

Readme

React Shared Store

State sharing with multiple components. You can save the state to localStorage or sessionStorage if you want and share between browser tabs.

npm downloads keywords author

Features

  • Create an dataStore Instance from that can be shared between multiple components.
  • Register components so that they automatically re-render when selected state properties change.
  • You can keep it in the browser using localStorage (or sessionStorage).
  • If desired, it performs state sync on the browser tabs.

Installation

yarn add react-shared-store

Code Demo

The following is an product/basket example of how to modify a property in a shared store object.

Create an Shared Store Instance

BasketStore.js

import SharedStore from "react-shared-store";

const BasketStore = new SharedStore(
    {
        basketItems: []
    }
);

export default BasketStore;

Create a new instance with initial state values. And export it.

Basket component

Basket.jsx

import BasketStore from "./BasketStore.js";

export default class Basket extends React.Component {

    componentDidMount() {
        BasketStore.register(this, "basketItems") // or multiple keys ["basketItems","totalPrice"]
    }

    componentWillUnmount() {
         BasketStore.unregister(this)
    }

    render() {
        const {basketItems} = BasketStore.state;
        return <ul>
           basketItems.map((item, key) =>
               <li key={key}>
                {item.name} - {item.price}
               </li>
           )
        </ul>
    }
}

Need to register to monitor the changes of a property.

Listing component

Listing.jsx

import BasketStore from "./BasketStore.js";
import products from "./products.json";

export default class Listing extends React.Component {

    onAddProduct = (item) => {
        BasketStore.setState((prevState)=>{
            return {
                basketItems: [...prevState.basketItems, item]
            }
        });
    }

    render() {
        return <ul>
           products.map((item, key) =>
               <li key={key}>
                {item.name} - {item.price}
                <button onClick={()=>this.onAddProduct(item)}>Add To Basket</button>
               </li>
           )
        </ul>
    }
}

Store Updateing is easy. It is the same as using react's setState()

App component

app.jsx

import React from 'react';
import Basket from "./Basket.jsx";
import Listing from "./Listing.jsx";

function App() {
    return (
        <div className="App">
            <Basket />
            <Listing />
        </div>
    );
}
export default App;

Registering Components

React components will only re-render if they are registered with the shared state. To do this use the register() method. Registration should occur either in the constructor or componentDidMount.

ExampleStore.register(this: React.Component, keys: string | Array<string>); this - Use 'this' to pass the react component itself into the shared state. keys - The shared state's property/properties that you wish the react component to re-render when updated.

Unregistering Components

All registered components need to unregister() before they are unmounted to remove listeners and prevent memory leaks. This is best done in componentWillUnmount.

ExampleStore.unregister(this: React.Component); this - Use 'this' to pass the react component itself into the shared state.

Getting state properties

The state property get the current state. Warning: do not mutate the state directly as this will not cause components to re-render

const { exampleProp } = ExampleStore.state; Using object destructuring allows quick and clean access to state properties.

Setting state properties

Like react components, shared states are updated using the setState() method, which accepts a partial state, updating corresponding state values.

ExampleStore.setState(partialState: object); partialState - An object of key/value pairs. Functions can be used as values to manipulate current property values (see setting with functions below).

Reset Properties

State can be easily returned to its default state using the reset() method.

ExampleStore.reset();

Advanced Features

  • Save state to storage (session or local) ExampleStore.useStorage(name: <string>, options: object); default options { type: "local", delay: 200 };

  • Share state between browser tabs option shareOnTabs: true Note: You can use this feature If type is defined as 'local' only.


Contributing

If you want to contribute to a project and make it better, your help is very welcome. Contributing is also a great way to learn more about social coding on Github, new technologies and and their ecosystems and how to make constructive, helpful bug reports, feature requests and the noblest of all contributions: a good, clean pull request.