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-persist-store

v1.0.16

Published

⚡ A persistent state management library for React. Create your own hooks that share data across components

Downloads

17

Readme

MIT License MIT License

Create your own hooks that share state. Initialise your store with default values and use an API that infers type information for you.

Features

  • Persistence: data is serialised and persisted to local storage by default, and hydrated automatically
  • Type safety: full TypeScript integration with type inference from default values
  • Reactive design: state updates are shared across components
  • Simplicity: does not require any component wrapping, or use of other abstractions such as the Context API

Getting Started

At a high level, it is generally suggested to create a store.ts file in which you set up your exported hook functions. Then these can be imported and used in components.

Installation

  1. Install NPM package

    npm install react-persist-store
  2. Set up your Store and create your hook

    import createStore, { Store } from "react-persist-store";
    
    const defaultValues: Store = {
      user: {
        firstName: "",
        lastName: "",
        badges: [], // In TypeScript this is never[], you can change this behaviour with createStore<YourStoreType>(...)
      },
      posts: [
        /** ... */
      ],
    };
    
    // You can pass options to customise the type of storage, "local", "session", or false to disable persistence
    // const store = createStore(defaultValues, { storage: 'session', namespace: 'custom' });
    const createHook = createStore(defaultValues);
    
    export const useUser = createHook("user")
  3. Use the hook anywhere in your application

    import { useUser } from "./store"
    
    const Component = () => {
      // Hooks do not take arguments, and return only:
      //  data - your data with types inferred from your store, or the generic you passed in
      //  update - a function what takes a partial copy of data to update
      //  clearAll - clear all state, including browser storage for this hook
      const { data, update, clearAll } = useUser()
      const { firstName, lastName } = data
      const fullName = `${firstName}${lastName ? ' ' + lastName : ''}`
      return (
        <p>
          {fullName}
        </p>
      )
    }
    
    export default Component

Usage and How It Works

The library exports a default function which takes a type of Store. This is just an object where keys are a namespace to a Document.

A Document is any object that can be serialised into JSON. That is, any data type that is one of null, number, string, boolean, or an array or object containing any combination of these.

This limitation is enforced, and this library is not suitable for storing data that is not serialisable.

When you call createStore you get back a function that can create hooks.

import createStore, { Store, Document } from 'at-your-service'

const document: Document = { example: '' }
const defaultValues: Store = {
  namespaceName: document
}

const createHook = createStore(defaultValues);

You can create as many namespace names as you wish. Each refers to a Document type.

The next step is to create hooks themselves. When you call createHook above, a closure is created that contains an event emitter. This event emitter is shared under the hood to users of the hook, and permits them to share updates to state.

The name that you pass to createHook is restricted to keyof typeof defaultValues. In other words, it has to be a key (a namespace name) in the top level of the default values passed to createStore.

export useStore = createHook("namespaceName")

Each created hook is called without arguments. Once called, no further action is required on the part of the hook.

Unless disabled, the hook first attempt to hydrate state from browser storage, based on its namespace name. This gets it up to date with the current state as updates to state are synchronised with browser storage. If this does not exist, then state is initialised from the default values.

It then returns an object with three properties:

  • data: is your (typed) data
  • update: takes a partial or full copy of your data and updates component state and local storage
  • clearAll: clears browser state associated with the hook and resets data to the default value
import { useUser } from "./<file_exporting_hook>"

const Component = () => {
  const { data, update, clearAll } = useUser()
  return <p>{data.example}</p>
}

License

Distributed under the MIT License. See LICENSE for more information.