react-session-provider
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 |