@vinsjo/use-local-storage
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

@vinsjo/use-local-storage

@vinsjo/use-local-storage a react hook for storing a state in localStorage

Package is bundled using microbundle

Installation

npm i @vinsjo/use-local-storage

Usage

import useLocalStorage from '@vinsjo/use-local-storage';

const App = () => {
    const [storedState, setStoredState] = useLocalStorage(
        // storageKey:
        // key to item in localStorage
        'my-storage-key',
        // initialState:
        // optional parameter with initial state or function returning initial state.
        // only stored in localStorage if nothing is already stored with the provided
        // key OR overwrite is set to true.
        // default value is null
        { foo: 'bar' },
        // overwrite:
        // optional parameter indicates if initialState should overwrite already
        // stored value.
        // default value is false
        false
    );

    return <>{storedState.foo}</>;
};

Package Sidebar

Install

npm i @vinsjo/use-local-storage

Weekly Downloads

2

Version

1.0.8

License

MIT

Unpacked Size

12.7 kB

Total Files

7

Last publish

Collaborators

  • vinsjo