Home

Awesome

@dannyman/use-store

Custom React Hook to enable using useState across the whole React Application

NPM JavaScript Style Guide

Overview

usePersistedState and useSessionState hooks will allow you to create global state variables to share or get across your React application.

How it works

usePersistedState: will allow you to store data and access it anywhere on your React app, even with the app open on different tabs or windows (on the same browser). The data will persist even if the app gets closed (unless the localstorage is cleared, in this case the data will get destroyed).

useSessionState: works as the usePersistedState hook, but encapsules the data so it can be used only on a single client session (read this to understand when a new session is created) and it gets destroyed as soon as the user leaves the session.

Features

Install

npm install --save @dannyman/use-store

Use the latest version for better support. Or upgrade to @1.1.3 if you are still using v1

Usage

The usePersistedState and useSessionState hooks will receive the following parameters:

* EXPERIMENTAL FEATURE, you could experience some issues.

Example

import React from  'react';
import { usePersistedState, useSessionState } from  '@dannyman/use-store';

const  Example  = () => {
    const [name, setName] =  useSessionState('app:key:name','Danny', { isNew: true });
    const [message] =  usePersistedState('app:key:message', 'nice to meet you', { listen: true });

    return (
        <div>Hello {name},  {message}!</div>
        <input type="text" onChange={(e) => setName(e.target.value)} />
    );
};

Changelog

v2.1.0

v2.0.9

v2.0.6

v2.0.4

v1.1.3

License

MIT © danny-fallas