npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-state-local-storage

v1.0.2

Published

React hook handler for localstorage persistence

Downloads

1

Readme

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:

  • data: T | null: The data stored with baseName as key base name, with provided type or any. This is a state, so you can manager component rendering whenever it changes. This will be null if LocalStorage is clear;

  • setData(data: T | null): void: The function that storing each prop of data param as a LocalStorage item with baseName as key base name.

  • setItem<I>(key: string, item: I): void: The function that storing item param inside the LocalStorage data with baseName as the key base name. The key param must be exactly the name of property you want to store;

  • removeItem(key: string): void: The function that removing a item stored inside the LocalStorage data with baseName as the key base name. The key param must be exactly the name of property you want to remove;

  • clearStorage(): void: The function that CLEANS ALL (KEEP IN MIND) your application's LocalStorage.

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