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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-multistore

v1.0.0

Published

A lightweight model to manage a global store using React hooks

Downloads

4

Readme

react-multistore

A lightweight library to manage a global store using React hooks

react-multistore enables to manage the state of a react app in a similar fashion as redux, i.e. by creating a single store which can be composed of multiple individual stores, and which uses actions (aka reducers) to modify the state.
The main difference is that react-multistore is a pure React solution in that it uses only the new react hooks. The library has no dependencies besides React and weighs only a few Kb before gzip.

Install

npm install --save react-multistore

Usage

The below demonstrates the basic usage of the library, from the simplest use-case to the most advanced.

Create a standalone hook

At its core, react-multistore relies on useApi which is a simple wrapper around react's useReducer that enables using an 'apiFactory' to update the state while preventing unnecessary re-renders:

import React from "react"
import { useApi } from "react-multistore"

const initialState = { likes: 0 }
const apiFactory = (state, setState) => ({
  ...state,
  like() {
    setState({ likes: state.likes + 1 })
  },
})

function MyComponent() {
  const { likes, like } = useApi(apiFactory, initialState)
  return (
    <div>
      <h2>{`Likes: ${likes}`}</h2>
      <button onClick={() => like()}>Like</button>
    </div>
  )
}

The state managed by useApi is only available in the component that uses the hook. To share the state among multiple components, create a store instead (see below).

Create a shared store

To share state and apiFactory among components, you create a store using createStore and make use of the useStore hook and the store's Provider component as follow:

import React from "react"
import ReactDOM from "react-dom"

const [useStore, StoreProvider] = createStore(
  (state, setState) => ({
    ...state,
    like() {
      setState({ likes: state.likes + 1 })
    },
  }),
  { likes: 0 } // Initial state
)

function MyComponent() {
  const { likes, like } = useStore()
  return (
    <div>
      <h2>{`Likes: ${likes}`}</h2>
      <button onClick={() => like()}>Like</button>
    </div>
  )
}

ReactDOM.render(
  <StoreProvider>
    <MyComponent />
  </StoreProvider>,
  document.getElementById("root")
)

Create a multistore

To combine multiple stores (e.g. to split your code into logical modules), you register individual stores into a multistore as follow:

import { createMultiStore } from "react-multistore"
import { useLikeStore, LikeStoreProvider } from "./Likes"
import { useViewStore, ViewStoreProvider } from "./Views"

const [useMultiStore, MultiStoreProvider] = createMultiStore({
  likes: LikeStoreProvider,
  views: ViewStoreProvider,
})

function App() {
  const { likes } = useLikeStore()
  const { views } = useViewStore()
  return (
   <h2>{`Likes/view: ${
      views !== 0 ? (likes / views).toPrecision(2) : "no views yet"
    }`}</h2>
  )
}

ReactDOM.render(
  <MultiStoreProvider>
    <App />
  </MultiStoreProvider>,
  document.getElementById("root")
)

There may be situations in which an individual store needs to be registered dynamically (e.g. when loaded on demand with code splitting). To do so, you can use the useMultiStore hook in the dynamically-loaded component. See below for an example.

Example

Check the example folder for a live demonstration of the various methods of react-multistore. To run it locally, simply use npm start and navigate to http://localhost:9000 on your browser.
The example is a dummy application with a Likes and a Views counters, demonstrating:

  • the Likes counter uses a store registered immediately within the multistore
  • the Views counter defines its store dynamically and uses the likes store
  • the App component uses a component-specific hook to toggle the View counter
  • The Likes view is not re-rendered upon modification of the views count

License

react-multistore is MIT licensed