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

redux-local-persist

v0.1.0

Published

Redux middleware for selectively loading and saving state to localStorage.

Downloads

121

Readme

Redux Local Persist

Redux middleware for selectively loading and saving state to localStorage. Redux Local Persist allows you to add a persist property to your Redux state that will specify how the defined values are saved in localStorage.

Applying the Middleware

import todos from './reducers';
import initialState from './state';
import { load, save } from 'redux-local-persist';
import { createStore, applyMiddleware } from 'redux';


// Load the persistent state from localStorage
let state = {
    ...initialState,
    ...load({initialState: initialState})
};

const store = createStore(
  todos,
  state,
  applyMiddleware(save())
);

store.dispatch({
  type: 'ADD_TODO',
  text: 'Understand the middleware'
});

If the initialState is persisting the text property, it will be saved to localStorage after store.dispatch is executed.

Methods

clear({ properites:String|Array [Optional] })

Clears the Redux Local Persist values from localStorage.

/**
 * Given the state:
    let initialState = {
        count: 0,
        deep: {
            nested: {
                value: 123
            }
        },
        loaded: true,
        persist: true,
    };
 */

...
import { clear } from 'redux-local-persist';

clear();                                // returns: {}
clear('deep.nested.value');             // returns: {count: 0, loaded: true}
clear(['count', 'deep.nested.value']);  // returns: {loaded: true}

load({ intialState:Object })

Gets the Redux Local Persist values from localStorage. If the values have expired, the initialState value is used.

save()

Automatically saves state changes to localStorage.

The Persist Property

The persist property can be of type: Boolean|Number|String|Array|Object

Persist as a Boolean

When set to true, the entire state tree for the connected component will be saved indefinitely.

Persist as a Number

The entire state tree for the connected component will be saved for the specified number of milliseconds. The next time the state is loaded from window.localStorage, the timestamp of the last save will be checked against the current time and if it's past the duration the Object Path will not be included.

Persist as a String

Only the specified Object Path of the state tree for the connected component will be saved indefinitely.

Example: deep.nested.value would persist a state tree of:

{
    deep: {
        nested: {
            value: 123
        }
    }
}

Persist as an Array of Strings

Only the specified Object Paths of the state tree for the connected component will be saved indefinitely.

Example: ['deep.nested.value', 'count'] would persist a state tree of:

{
    count: 0,
    deep: {
        nested: {
            value: 123
        }
    }
}

Persist as an Object

Only the specified Object Paths of the state tree for the connected component will be saved for the specified number of milliseconds.

Example: {'deep.nested.value': 10000, 'loaded': 30000} would persist a state tree of:

{
    loaded: false,
    deep: {
        nested: {
            value: 123
        }
    }
}

Usage

Simple

Persist the entire state tree indefinitely:

...
let initialState = {
    count: 0,
    loaded: false,
    persist: true
};
...

Simple Usage with Expiration

Persist the entire state tree for 10 seconds:

...
let initialState = {
  count: 0,
  loaded: false,
  persist: 10000,
};
...

Save Specific Parts of State Tree

Persist only deep.nested.value and loaded values indefinitely:

...
let initialState = {
  count: 0,
  loaded: false,
  deep: {
      nested: {
          value: 123
      }
  }
  persist: ['deep.nested.value', 'loaded'],
};
...

Save Specific Parts of State Tree with Expiration

Persist only the deep.nested.value for 10 seconds, loaded for 5 seconds, and count indefinitely:

...
let initialState = {
  count: 0,
  loaded: false,
  deep: {
      nested: {
          value: 123
      }
  }
  persist: {
      'deep.nested.value': 10000,
      'loaded': 5000,
      'count': true
  },
};
...