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

@locmod/local-storage

v1.0.4

Published

local, session and memory storage manager

Downloads

358

Readme

@locmod/local-storage

Use this package to control localStorage and sessionStorage. It handles all possible errors and provides a fallback in form of MemoryStorage.

This package works only on client side and will throw an error if you use try to use it on server-side. More info in SSR section.

Usage

Import default export from local-storage package and use directly.

import React, { useEffect, useMemo } from 'react'
import localStorage from 'local-storage'

const StoragePage = () => {
  if (__SERVER) {
    return null 
  }

  const { value = 1, lastUpdate } = localStorage.getSessionItem<{ value: number, lastUpdate: string }>('counter') || {}

  useEffect(() => {
    localStorage.setSessionItem('counter', { value: value + 1, lastUpdate: (new Date).toISOString() })
  }, [])

  return (
    <div>Local storage: {value}, lastUpdate: {lastUpdate}</div>
  )
}

export default StoragePage

If you just want to update localStorage item, please read the previous value inside callback:

useEffect(() => {
  const { value = 1 } = localStorage.getSessionItem<{ value: number }>('counter') || {}

  localStorage.setSessionItem('counter', { value: value + 1, lastUpdate: (new Date).toISOString() })
}, [])

SSR

If your page depends on localStorage we can't render it on the server side, because of unknown state. So you should disable component's render on server side:

const StoragePage = () => {
  if (__SERVER) {
    return <StoragePageSkeleton />
  }

  // client side rendering
  const { value = 1, lastUpdate } = localStorage.getSessionItem<{ value: number, lastUpdate: string }>('counter') || {}

  return (
    ...
  )
}    

API

getItem

Return item from localStorage by key. You can pass types for the value.

const counter = localStorage.getItem<number>('counter')

Library doesn't provide default values. You need to do it by your own.

const { step = 0, finished = false } = localStorage.getItem<{ step: number, finished: boolean }>('data') || {}

setItem

Saves value to localStorage by key. You can save primitives and objects.

localStorage.setItem<number>('counter', 42)
localStorage.setItem<{ value: number, lastUpdate: string }>('data', { 
  value: 2, 
  lastUpdate: new Date().toISOString(), 
})

updateItem

localStorage.updateItem<{ value: number, lastUpdate: string }>('data', (currItem) => ({
  value: currItem.value + 1,
  lastUpdate: new Date().toISOString(),
}))

removeItem

Removes item from localStorage by key.

localStorage.removeItem('data')

getKeys

Returns key list stored in localStorage. It can be useful for removing items by prefixes.

const keys = localStorage.getKeys()

// keys = [ 'counter', 'data' ]

getSessionItem

The same as getItem but for sessionStorage.

setSessionItem

The same as setItem but for sessionStorage.

updateSessionItem

The same as setItem but for sessionStorage.

removeSessionItem

The same as removeItem but for sessionStorage.

getSessionKeys

The same as getKeys but for sessionStorage.