react-sessionstorage

1.0.5 • Public • Published

react-sessionstorage

You can use session storage with hooks. Since it is shared in the context, it will be re-rendered when the dependencies are updated.

install

npm install react-sessionstorage

How to use

import React from "react";
import ReactDOM from "react-dom";
import {
  SessionStorageProvider,
  useSessionStorage,
} from "react-sessionstorage";

const Component = () => {
  return (
    <SessionStorageProvider>
      <ChildComponent />
    </SessionStorageProvider>
  );
};

function ChildComponent() {
  const [
    sessionStorages,
    setSessionStorage,
    removeSessionStorage,
  ] = useSessionStorage(["test"]);
  const setItem = () => {
    setSessionStorage("test", "hoge");
  };

  return <button onClick={setItem}>{sessionStorages.test}</button>;
}

ReactDOM.render(<Component />, document.getElementById("app"));

useSessionStorage

response

[sessionStorage,setSessionStorage,removeSessionStorage]

arguement

string[] Put the session storage key in the array. Rerendered when the sessionStorage keys contained in the array are updated.

setSessionStorage

argument

setSessionStorage("sesssionStoragekey","value") Set sessionStorage.

removeSessionStorage

argument

removeSessionStorage("sesssionStoragekey") remove sessionStorage.

Readme

Keywords

Package Sidebar

Install

npm i react-sessionstorage

Weekly Downloads

9

Version

1.0.5

License

MIT

Unpacked Size

14.3 kB

Total Files

11

Last publish

Collaborators

  • islandryu