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

use-web-storage-api

v1.0.11

Published

Simple react hooks to use the Web Storage API (https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)

Downloads

17

Readme

use-web-storage-api

Simple react hook that saves the value to Web Storage

Install:

npm i use-web-storage-api

Usage:

By default, the hook looks very similar to useState, except that it receives an additional argument, options.

Here is the default usage:

import useWebStorage from 'use-web-storage-api';

const MyComponent: React.FC = () => {
    const [name, setName] = useWebStorage('Bob', {
        key: 'NAME', // key of the value in local/session storge
    });

    // When button is clicked, the value in storage will change to 'John'
    return (
        <div>
            <h3>{name}</h3>
            <button onClick={() => setName('John')}>Change name</button>
        </div>}
    );
}

Options can have following keys:

export interface IUseWebStorageOptions<T> {
    // The key under which the value will be stored
    key: string;
    // Storage type, default local
    storageType?: 'local' | 'session';
    // Serialization function: default JSON.stringify
    serialize?: (val: T) => string;
    // Deserialization function: default just returns the string or calls JSON.parse for objects
    deserialize?: (val: string) => T;
    // Number of milliseconds to expiration. Default doesn't expire
    expiresIn?: number;
}

Example

If you have a Date value to be stored in the localstorage for max 8 hours, it could be achieved like this:

import useWebStorage from 'use-web-storage-api';

const MINUTE = 1000 * 60;
const HOUR = MINUTE * 60;

const MyComponent: React.FC = () => {
    const [date, setDate] = useWebStorage(new Date(), {
        key: 'DATE', 
        serialize: (dt) => dt.toISOString(),
        deserialize: (dtStr) => new Date(dtStr),
        expiresIn: HOUR * 8,
    });

    // When button is clicked, the value in storage will change to 'John'
    return (
        <div>
            <h3>{date.toISOString()}</h3>
            <button onClick={() => setDate(new Date())}>Change name</button>
        </div>}
    );
}