react-session-provider
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

react-session-provider

npm version npm NPM

Component to handle localStorage session easily.

Installation

yarn add react-session-provider
# or
npm i -s react-session-provider

Usage

app.js

import SessionProvider from 'react-session-provider';

const App = () => {
    return (
        <SessionProvider pKey=""
            pKey="my-encryption-key"
            sessionName = "a-name-for-your-session",
        >
            {... my other components}
        </SessionProvider>
    );
};

To access session variables use the hooks

import {useSession} from 'react-session-provider';

const MyComponent = () => {
    const {store = {}, setKey} = useSession();
    const {hello} = store;
    const handleClick = () => {
        setKey('hello', 'Hello world!');
    };
    return (
        <div>
            <p>{hello}</p>
            <button onClick={handleClick}>say hello</button>
        </div>
    );
};

IMPORTANT: Under the hood, this lib makes use of hooks, therefore, using it requires React >= 16.8.

useSession hook will provide you the store, and functions to manipulate it

Name Type Default description
setKey function Allows to add or modify a session key
deleteKey function Allows to remove a key from the store
setAll function Allows to set multiple keys at once
store object {} Object containing your keys

Package Sidebar

Install

npm i react-session-provider

Weekly Downloads

10

Version

1.1.0

License

MIT

Unpacked Size

67.7 kB

Total Files

28

Last publish

Collaborators

  • alejandro.devop