zach-local-storage-safe

power up window localStorage, with obj, array, string support, initialize data type, expire time

Usage no npm install needed!

<script type="module">
  import zachLocalStorageSafe from 'https://cdn.skypack.dev/zach-local-storage-safe';
</script>

README

zach-local-storage-safe

Power up browser localStorage function, with extra obj, array, string support, initialize data type, expire time.

NPM JavaScript Style Guide

Table of Contents

Install

# Yarn
yarn add zach-local-storage-safe

# NPM
npm install --save zach-local-storage-safe

Quick Start

// state initilize with localStorage
import {useState,useCallback} from 'react';
import locaslStorageSafe from 'zach-local-storage-safe';
const Example = () => {
    const [state,setState] = useState(locaslStorageSafe.getItem('myState','object') );
    const _onClick = useCallback((e)=>{
            const id = e.target.getAttribute('data-id');
             const data = {id};
             locaslStorageSafe.setItem('myState',data,30000);
            setState(()=>data)
    },[])
    return (
        <div onClick={_onClick}  data-id={state.id}>
            Test
        </div>
    );
};

Usage

List browsers supported

Related projects

Thanks

This repo was setup with the help of the excellent create-react-library and redux-react-hook.

Contributing

Zach Yu zachyu.tw@gmail.com

License

MIT