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

application-storage

v1.0.0

Published

Save and load objects and state easily to either session storage or local storage. Works great inside or outside React.

Downloads

10

Readme

Application Storage

Easy to use package for saving and loading data to the application storage, both local and session. Works great in whatever Javascript/Typescript framework you are working in without any dependencies.

Installation

$ npm i application-storage

Examples

Javascript Example

This is a basic example of saving and loading a username so that it will remember what they entered last time. The first time this code runs it will prompt the user for their name with a blank default value. The second time it runs it will prompt the user for their name and will default to the value they entered last time. After they enter their name it will update the value.

import appStorageFactory from 'application-storage';

var appStorage = appStorageFactory("nameOfPerson", "local");
var name = prompt("What is your name", appStorage.getValue());
appStorage.setValue(name);

React/Typescript Example

This makes saving and loading values in React effortless, which is great for components where you want to remember what the user had before, such as viewing preferences or customizations.

import * as React from 'react';
import appStorageFactory from 'application-storage';

export class RememberName extends React.Component<any> {
    readonly appStorage = appStorageFactory("name", 'local');
    onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        this.appStorage.setValue(event.target.value);
        // Some event that causes a re-render
    }

    render() {
        return (
            <div>
                <label>Name: </label>
                <input value={this.appStorage.getValue()} onChange={this.onChange} />
            </div>
        );
    }
}

It is just the three parts, getting an instance for your class, saving the value with an onChange event and using it as the value for the object. The gotcha is setting the value will not cause a re-render, so inside of the event you have to do something here that causes a re-render. This would normally be an update to state that tracks name.

React/Typescript with State

Here is a more realistic example with the user able to enter a form field and have it save to state, so you can act upon it in your code and do what you need with it in a normal React way, but also caching it to the storage as shown above:

import * as React from 'react';
import appStorageFactory from 'application-storage';

export class RememberName extends React.Component<any, { name: string }> {
    readonly appStorage = appStorageFactory("name", 'local');

    state = {
        name: this.appStorage.getValue()
    }

    onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        this.setState({ name: event.target.value });
        this.appStorage.setValue(event.target.value);
    }

    render() {
        return (
            <div>
                <label>Name: </label>
                <input value={this.state.name} onChange={this.onChange} />
            </div>
        );
    }
}

Requirements

The only requirement is that the browser supports localStorage and sessionStorage, which has been around for a while, and is supported as far bas as IE8. For a full list of browser compatibility visit https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage.