1.1.0 • Published 3 years ago

react-session-provider v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

NameTypeDefaultdescription
setKeyfunctionAllows to add or modify a session key
deleteKeyfunctionAllows to remove a key from the store
setAllfunctionAllows to set multiple keys at once
storeobject{}Object containing your keys
1.1.0

3 years ago

1.0.2-beta.2

3 years ago

1.0.2-beta.1

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago