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-localstorage-fix-localstorage-fork

v1.0.0-rc6

Published

Unopinionated store enhancer that persists state changes (locally).

Downloads

6

Readme

Deprecated

This fork is deprecated. #32 is closed and a new upstream version published.

This is a mega-hack because I needed to use this on React Native and there hasn't been a version released with the fix.

This will go away when #32 is resolved.

Original readme follows.

redux-localstorage

Unopinionated store enhancer that persists state changes (locally).

Similarly to redux, redux-localstorage has a small API footprint yet provides great flexibility by embracing functional composition. Through functional composition you can enhance your persistence layer of choice to meet your specific needs!

license npm version npm downloads

Installation

npm install --save redux-localstorage

The Gist

import {compose, createStore} from 'redux';
import rootReducer from './reducers';

import persistState, {mergePersistedState} from 'redux-localstorage';
import adapter from 'redux-localstorage/lib/adapters/localStorage';
import filter from 'redux-localstorage-filter';

const reducer = compose(
  mergePersistedState()
)(rootReducer);

const storage = compose(
  filter('nested.key')
)(adapter(window.localStorage));

const createPersistentStore = compose(
  persistState(storage, 'my-storage-key')
)(createStore);

const store = createPersistentStore(reducer);

API

persistState([storage][, key][, callback])

storage

type storage = Storage (Object)

An object that provides (enhanced) methods for data persistence, retrieval and removal as put, get & del. Defaults to adapter(localStorage).

key

type key = String

The key used to store (and retrieve) persisted state. Defaults to 'redux-localstorage'.

callback

type callback = Function

Called when persistState has finished initializing (after rehydration).

mergePersistedState([merge])

mergePersistedState is a higher order reducer that can be used to rehydrate the store. It takes a function (optional) that defines how the application's initial state should be merged with any persisted state.

merge

type merge = (initialState, persistedState) => mergedState

Function that defines how the persisted state should be merged with the initial state. By default mergePersistedState performs a shallow merge. The following shows how you can easily define a deep merge using e.g. lodash.merge:

const reducer = compose(
  mergePersistedState((initialState, persistedState) => {
    return _.merge({}, initialState, persistedState)
  }),
)(rootReducer);

Note: The initialState includes the default values specified by your reducers.

Rehydration

The use of mergePersistedState is optional. If you prefer to handle rehydration in your own reducer(s), you can. Listen for redux-localstorage's INIT action; it includes the persisted state as it's payload. For example:

import {actionTypes} from 'redux-localstorage'

export default function reducer(state = {}, action) {
  if (action.type === actionTypes.INIT) {
    const persistedState = action.payload['reducer']
    return {...state, ...persistedState}
  }
//...

Note: The INIT action is passed on to your reducers by mergePersistedState in case you would like to set an initialised flag for example.

Storage

Redux-localstorage can be made to work with any storage implementation - it doesn't even have to be local! All that is required is that the storage that is passed in exposes the following methods.

storage = {
  put(key, value, callback) {},
  get(key, callback) {},
  del(key, callback) {}
};

A number of adapters are provided to wrap existing storage API's so that they conform to these requirements. But you could create your own storage object and point these methods to any endpoint you like!

adapters

Redux-localstorage currently provides adapters for localStorage, sessionStorage and AsyncStorage. An adapter creates a thin wrapper that transforms a storage API so that it conforms to the stated requirements. The original storage object passed to an adapter can be accessed through adapted[0]; this provides you access to all the original storage methods when creating a storage enhancer.

import {AsyncStorage} from 'react-native';
import adapter from 'redux-localstorage/lib/adapters/AsyncStorage';

const storage = adapter(AsyncStorage);
// storage[0] === AsyncStorage

enhancers

type enhancer = (Storage) => Storage

Through functional composition it's really easy to enhance a storage object. This provides a lot of flexibility, allowing for fun stuff like:

const storage = compose(
  debounce(100),
  filter(['key', 'another.key']),
  errorHandling,
  yourOwnCustom(enhancer)
)(adapter(window.localStorage));

const createPersistentStore = compose(
  persistState(storage, 'my-storage-key')
)(createStore);

Check out the wiki for a list of available storage enhancers and don't forget to add your own if you publish any!

License

MIT