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-simple-storage

v1.4.2

Published

Simple component and helper functions for using web storage with React.

Downloads

316

Readme

React Simple Storage

A simple component and helper functions for using web storage with React.

Check out the demo app and basic example. https://ryanjyost.github.io/react-simple-storage-example-project

You may also want to check out my related Hacker Noon article, How to take advantage of Local Storage in your React projects, for some context and logic behind this project.

Good use cases for react-simple-storage

  • Persist and experiment with a component's state while developing.
  • Save form data across user sessions.
  • A simple, quick fake backend for a practice or portfolio project.
  • More I can't think of...

Install

Install via yarn.

yarn add react-simple-storage

Using on IE11

For react-simple-storage to work on IE11, you'll need to use babel-polyfill.

yarn add babel-polyfill

Then import in your project.

import "babel-polyfill";

Usage

Component

Import and include an instance of react-simple-storage in a component whose state you want to save to web storage.

import React, { Component } from "react";
import SimpleStorage from "react-simple-storage";

export default class ParentComponent extends Component {
  constructor(props) { 
    super(props)
    this.state = {
      text: "",
    }
  }

  render() {
    return ( 
      <div>
      
        // include the component somewhere in the parent to save the parent's state in web storage
        <SimpleStorage parent={this} />

        // the value of this input will be saved in web storage
        <input
          type="text"
          value={this.state.text}
          onChange={e => this.setState({ text: e.target.value })}
        />
        
      </div>
    ) 
  }
}

Props

| Name | Type |Required? | Default | Description | ---------------- |:--------------- |:-------- | ------------ |------------- | parent | object | Yes | none | reference to the parent component, i.e. this | prefix | string | No | "" | prefix added to storage keys to avoid name clashes across instances
| blacklist | array | No | [] | a list of parent component's state names/keys to ignore when saving to storage | onParentStateHydrated | func | No | none | fires after the parent component's state has been updated with storage items. Basically a callback for working with the parent component's state once updated with storage.

Helper Functions

clearStorage(prefix)

Clears items in storage with the given prefix, or all items if no prefix is given.

  • prefix: String | optional - Corresponds to prefix prop passed to an instance of the react-simple-storage component.

Example

import React, { Component } from "react";
import SimpleStorage, { clearStorage } from "react-simple-storage";

export default class ParentComponent extends Component {
  constructor(props) { 
    super(props)
    this.state = {
      text: "",
    }
  }

  render() {
    return ( 
      <div>
      
        // provide a prefix prop to be able to clear just the storage items 
        // created by this instance of the react-simple-storage component
        <SimpleStorage parent={this} prefix={"ParentComponent"} />

        <input
          type="text"
          value={this.state.text}
          onChange={e => this.setState({ text: e.target.value })}
        />
        
        // removes only storage items related to the ParentComponent
        <button onClick={() => clearStorage("ParentComponent")}>
          Clear storage for ParentComponent
        </button>
        
         // removes all items from storage
        <button onClick={() => clearStorage()}>
          Clear all storage
        </button>
        
      </div>
    ) 
  }
}

resetParentState(parent, initialState, keysToIgnore)

Resets the parent's state to given initialState.

  • parent: Object | required - Reference to the parent component, allowing react-simple-storage to access and update the parent component's state. If called within the parent component, simply pass this.
  • initialState: Object | required - The state of the parent component after the function executes.
  • keysToIgnore: Array | optional - A list of keys in the parent component's state to ignore on resetParentState . These pieces of that parent's state will NOT be reset.

Example

import React, { Component } from "react";
import SimpleStorage, { resetParentState } from "react-simple-storage";

export default class ParentComponent extends Component {
  constructor(props) { 
    super(props)
    this.state = {
      text: "Initial Text",
    }
    
    // store the component's initial state to reset it
    this.initialState = this.state;
  }

  render() {
    return ( 
      <div>
      
        <SimpleStorage parent={this} />

        <input
          type="text"
          value={this.state.text}
          onChange={e => this.setState({ text: e.target.value })}
        />
        
        // will set "text" in state to "Initial Text"
         <button onClick={() => resetParentState(this, this.initialState)}>
           Reset parent state
         </button>
        
        // ignores "text" on reset, so will have no effect here
        <button onClick={() => resetParentState(this, this.initialState, ['text'])}>
          Do NOT reset text
        </button>
        
      </div>
    ) 
  }
}

Built with

  • store.js - Cross-browser storage for all use cases, used across the web.

License

This project is licensed under the MIT License - see the LICENSE.md file for details