react-state-local-storage

A React TS hook that handles persistence in LocalStorage, returning a state you can use to perform component rendering.

Installation

You can install it with:

npm install react-state-local-storage

// or

yarn add react-state-local-storage

Import

import { useLocalStorage } from 'react-state-local-storage';

Usage

useLocalStorage(config: useLocalStorageConfig)

const storageData = useLocalStorage({
    baseName: "@yourAppName",
    initialData: {
        //...props
    }
});

Providing a type:

interface UserInterface {
    name: string;
    lastaName: string;
    age: string;
}

interface ParentsInterface {
    motherName: string;
    fatherName: string;
}

interface LocalStorageInterface {
    user: UserInterface;
    parents: ParentsInterface;
}

// {...}

const storageData = useLocalStorage<LocalStorageInterface>({
    baseName: "@yourAppName",
    initialData: {
        // LocalStorageInterface props
    }
});

Now, storageData has:

Examples

storageData.setData(data: T | null)

const user: {
    name: "Iam",
    lastName: "Costa",
    age: "24"
}

const parents: {
    motherName: "Francisca",
    fatherName: "Antônio",
}

storageData.setData({user, parents});
/* 
storageData.data.user = { name: "Iam", lastName: "Costa", age: "24" },
storageData.data.parents = {motherName: "Francisca", fatherName: "Antônio"}
*/

storageData.setItem<I>(key: string, item: I)

const user: {
    name: "Iam",
    lastName: "Costa",
    age: "24"
}

storageData.setItem<UserInterface>("user", user);
// storageData.data.user = { name: "Iam", lastName: "Costa", age: "24" }

storageData.removeItem(key: string)

storageData.removeItem("user");
// storageData.data.user = null

storageData.clearStorage()

storageData.clearStorage();
// storageData.data = null
/* any other key previously provided will return null because 
all of your app's localStorage has been cleared, so be careful */

Another way…

There are another hook you can use. The first version of this package with some limitations. Here a example:

import { useLocalStoragePrototype } from 'react-state-local-storage'

// {...}

// This handles storage only in the provided key
const storageItem = useLocalStoragePrototype("@app/key");

const user: {
    name: "Iam",
    lastName: "Costa",
    age: "24"
}

// given functions
storageItem.setItem(user);
storageItem.removeItem();
storageItem.clearStorage();

Note: In this case, although the clearStorage function clears the entire LocalStorage, it only returns the state (null value) of the given key, since one object of useLocalStoragePrototype handles only about it own key data.

Take a look

Clone repository below:

git clone https://github.com/iamcosta/react-custom-local-storage-hook.git

Install dependecies:

npm install

// or 

yarn

Run:

npm run start

// or 

yarn start

Open http://localhost:3000 to view it in the browser.

Send me feedback

This is my first package and I’m trying to put all my love on it <3

License

MIT @ Iam Barroso da Costa