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

persist-state

v1.2.0

Published

Persist react state after page refresh

Downloads

8

Readme

persist-state

Persist react state after page refresh

NPM JavaScript Style Guide

Intruduction

This is a library to provide a persistable state with React Hooks. It has following characteristics.

  • Persist state after page refresh.
    • The library cares the state object is persisted after the page has refresh.
  • State persist criteria can be configured
    • By default state will be persisted only when page is refreshed for the browser session (like sessionStorage).
    • It can be configure so that the state will be persisted after the component is unmounted.
    • It can be configured so that the state will be persisted across the browser sessions (like localStorage).
  • The hook is basically a wrapper for useState hook.
    • It has similar api like useState hook expects it accepts extra parameter that is config object.

Install

npm install --save persist-state

Usage

without configuration using only key

import React, { Component } from 'react'

import { usePersistState } from 'persist-state'

const Example = () => {
  const [count, setCount] = usePersistState(0, 'count')
  return (
    <div>
      Counter: {count}

      <button onClick={() => setCount((count) =>(count+1))}> Increment</button>

      <button onClick={() => setCount((count) =>(count-1))}> Decrement</button>
    </div>
  )
}

with configuration

import React, { Component } from 'react'

import { usePersistState } from 'persist-state'

const Example = () => {
  const [count, setCount] = usePersistState(0, {
    key: "count",
    persistOnUnmount: false,
    persistAcrosSession: true
  })

  return (
    <div>
      Counter: {count}

      <button onClick={() => setCount((count) =>(count+1))}> Increment</button>

      <button onClick={() => setCount((count) =>(count-1))}> Decrement</button>
    </div>
  )
}

API

usePersistState:

It has almost similar api like useState hook, except it accepts extra parameter.

Return:

It returns a pair of values: the current state and a function that updates it.

  • state: a custom hook works like React.useState
  • setState: a function to set a state like React.useState

Parameters:

  • initialState: State like React.useState. It also supports lazy initialization.
  • config: This is a config, it can be either object or a string.
    • string: To use default configuration only pass the key as string which should be unique in the project. It is mandatory to pass the key. One possible approach to create a unique key is componentName-variableName.
    • object: To configure the default behavior use following configuration fields.
      • key: This is unique string and mandatory field.
      • persistAcrosSession: This is optional and it's default value is false. Use this configuration key when you want persist the state across the session. The state will be stored permantly on user browser. One possible use case may to store the theme choose by user.
        • There are two possible values true and false
          • true: persist state across the sessions
          • false: persist state during the browser session.
      • persistOnUnmount: This is optional and it's default value is false. Use this configuration key when you want to presist the state when component is unmounted.
        • There are two possible values true and false
          • true: persist state even after component is unmounted.
          • false: doesn't persist state after component is unmounted.

License

MIT © Deepak Bansode


This hook is created using create-react-hook.