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-hook-session-web-storage

v2.0.1

Published

A React hook for accessing sessionStorage

Downloads

37

Readme

react-hook-session-web-storage :anchor:

A React hook to access sessionStorage.

Installation

Using npm:

npm install react-hook-session-web-storage

Using yarn:

yarn add react-hook-session-web-storage

Basic usage

The useSessionStorage hook, similarly to the useState hook, returns an array of two elements:

  • The first element contains the value stored in sessionStorage.

  • The second element is a function which can be called with a value which will be converted to string and stored in sessionStorage. If this function is called without an argument, or with the argument null or undefined, the sessionStorage entry will be removed.

The key to the sessionStorage entry you want to access must be supplied to the hook as its first argument.

For example:

import useSessionStorage from "react-hook-session-web-storage";

const ComponentWithSessionStorage = () => {
  const [value, setValue] = useSessionStorage("myKey");

  return (
    <div>
      <p>Value in SessionStorage: {value}</p>
      <button
        onClick={() => {
          setValue("Value from hook");
        }}
      >
        Set myKey
      </button>
      <button
        onClick={() => {
          setValue(null);
        }}
      >
        Unset myKey
      </button>
    </div>
  );
};

Using multiple sessionStorage entires

The useSessionStorage hook can work with multiple sessionStorage entries simultaneously.

In order to achieve this, the first argument specified when calling the hook must be an object instead of a string. The keys of the object will represent the sessionStorage entry keys used by the hook. The values specified in this object will be ignored, in order to avoid confusion, it is recommended to use null as values.

If the useSessionStorage hook is called like this, the array it returns will contain two elements:

  • The first element is an object which contains all values in the sessionStorage by the keys specified in the object the hook was called with.

  • The second element is a function which expects an object as a parameter, in which the keys must be a subset of the keys of the object the hook was called with. Every sessionStorage entry corresponding to a key in this object will be set to the value specified by that key (converted to string). If the value by any key is null or undefined, the corresponding sessionStorage entry will be unset.

For example:

import useSessionStorage from "react-hook-session-web-storage";

const ComponentWithSessionStorage = () => {
  const [values, setValues] = useSessionStorage({
    myKey: null,
    myOtherKey: null,
  });

  const { myKey, myOtherKey } = values;

  return (
    <div>
      <p>
        Values in SessionStorage: {myKey}, {myOtherKey}
      </p>
      <button
        onClick={() => {
          setValues({ myKey: "Setting myKey from hook" });
        }}
      >
        Set myKey, leave myOtherKey intact
      </button>
      <button
        onClick={() => {
          setValues({ myOtherKey: "Setting myOtherKey from hook" });
        }}
      >
        Set myOtherKey, leave myKey intact
      </button>
      <button
        onClick={() => {
          setValues({
            myKey: "Setting myKey from hook",
            myOtherKey: "Setting myOtherKey from hook",
          });
        }}
      >
        Set both myKey and myOtherKey
      </button>
      <button
        onClick={() => {
          setValues({
            myKey: null,
          });
        }}
      >
        Unset myKey, leave myOtherKey intact
      </button>
      <button
        onClick={() => {
          setValues({
            myOtherKey: null,
          });
        }}
      >
        Unset myOtherKey, leave myKey intact
      </button>
      <button
        onClick={() => {
          setValues({
            myKey: "Setting myKey from hook",
            myOtherKey: null,
          });
        }}
      >
        Set myKey and unset myOtherKey
      </button>
      <button
        onClick={() => {
          setValues({
            myKey: null,
            myOtherKey: null,
          });
        }}
      >
        Unset both myKey and myOtherKey
      </button>
    </div>
  );
};

Enable syncing

By default sessionStorage will be accessed only once when the component using the hook is mounted.

The hook can be configured to automatically react to changes of the sessionStorage entry or entries by calling it with a second options parameter, in which by the key syncFrequency the frequency of reading the data from sessionStorage can be specified in milliseconds.

This feature works the same way whether the hook is used with a single sessionStorage entry or multiple entires.

For example:

import useSessionStorage from "react-hook-session-web-storage";

const ComponentWithSessionStorage = () => {
  const [value, setValue] = useSessionStorage("myKey", { syncFrequency: 1000 });

  return (
    <div>
      <p>Value in SessionStorage: {value}</p>
      <button
        onClick={() => {
          setValue("Value from hook");
        }}
      >
        Set value with hook
      </button>
      <button
        onClick={() => {
          setValue(null);
        }}
      >
        Unset value
      </button>
    </div>
  );
};

Be aware that when the hook is used like this, the component will access sessionStorage at regular intervals, which can result in a delay in registering changes and lead to performance issues if the update frequency is low.

Limitations

sessionStorage is using the Storage interface of the Web Storage API which requires all keys and values to be strings.

Migrating from 1.x.x

If you were using default import, remember to specify the syncFrequency in the second options parameter when calling the hook to keep syncing with sessionStorage.

If you were using the named import useSessionStorageNoSync, simply switch to default import.

Contributions

Contributions are welcome. File bug reports, create pull requests, feel free to reach out at [email protected].

License

react-hook-session-web-storage is licensed under LGPL.