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-remember

v5.2.0

Published

Saves and loads your redux state from a key-value store of your choice

Downloads

16,425

Readme

NPM Version Build Status Coverage Status NPM Downloads

Logo

Redux Remember saves and loads your redux state from a key-value store of your choice.

Important

The current version of Redux Remember is tested working with [email protected]+ and [email protected]+. In case you want to use this library with an older versions of Redux or Redux Toolkit you might need to switch back to version 4.2.2 of Redux Remember.

Key features:

  • Saves (persists) and loads (rehydrates) only allowed keys and does not touch anything else.
  • Completely unit and battle tested.
  • Works on both web (any redux compatible app) and native (react-native).

Works with any of the following:

  • AsyncStorage (react-native)
  • LocalStorage (web)
  • SessionStorage (web)
  • Your own custom storage driver that implements setItem(key, value) and getItem(key)

See demo!

Installation

$ npm install --save redux-remember
# or
$ yarn add redux-remember

Usage - web

import { configureStore, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { rememberReducer, rememberEnhancer } from 'redux-remember';

const myStateIsRemembered = createSlice({
  name: 'persisted-slice',
  initialState: {
    text: ''
  },
  reducers: {
    setPersistedText(state, action: PayloadAction<string>) {
      state.text = action.payload;
    }
  }
});

const myStateIsForgotten = createSlice({
  name: 'forgotten-slice',
  initialState: {
    text: ''
  },
  reducers: {
    setForgottenText(state, action: PayloadAction<string>) {
      state.text = action.payload;
    }
  }
});

const reducers = {
  myStateIsRemembered: myStateIsRemembered.reducer,
  myStateIsForgotten: myStateIsForgotten.reducer,
  someExtraData: (state = 'bla') => state
};

export const actions = {
  ...myStateIsRemembered.actions,
  ...myStateIsForgotten.actions
};

const rememberedKeys = [ 'myStateIsRemembered' ]; // 'myStateIsForgotten' will be forgotten, as it's not in this list

const reducer = rememberReducer(reducers);
const store = configureStore({
  reducer,
  enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(
    rememberEnhancer(
      window.localStorage, // or window.sessionStorage, or your own custom storage driver
      rememberedKeys
    )
  )
});

// Continue using the redux store as usual...

Usage - react-native

import AsyncStorage from '@react-native-async-storage/async-storage';
import { configureStore, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { rememberReducer, rememberEnhancer } from 'redux-remember';

const myStateIsRemembered = createSlice({
  name: 'persisted-slice',
  initialState: {
    text: ''
  },
  reducers: {
    setPersistedText(state, action: PayloadAction<string>) {
      state.text = action.payload;
    }
  }
});

const myStateIsForgotten = createSlice({
  name: 'forgotten-slice',
  initialState: {
    text: ''
  },
  reducers: {
    setForgottenText(state, action: PayloadAction<string>) {
      state.text = action.payload;
    }
  }
});

const reducers = {
  myStateIsRemembered: myStateIsRemembered.reducer,
  myStateIsForgotten: myStateIsForgotten.reducer,
  someExtraData: (state = 'bla') => state
};

export const actions = {
  ...myStateIsRemembered.actions,
  ...myStateIsForgotten.actions
};

const rememberedKeys = [ 'myStateIsRemembered' ]; // 'myStateIsForgotten' will be forgotten, as it's not in this list

const reducer = rememberReducer(reducers);
const store = configureStore({
  reducer,
  enhancers:  (getDefaultEnhancers) => getDefaultEnhancers().concat(
    rememberEnhancer(
      AsyncStorage, // or your own custom storage driver
      rememberedKeys
    )
  )
});

// Continue using the redux store as usual...

Usage - inside a reducer

import { createSlice, createAction, PayloadAction } from '@reduxjs/toolkit';
import { REMEMBER_REHYDRATED, REMEMBER_PERSISTED } from 'redux-remember';

const initialState = {
  isRehydrated: false,
  isPersisted: false
};

const reduxRemember = createSlice({
  name: 'redux-remember',
  initialState,
  reducers: {},
  extraReducers: (builder) => builder
    .addCase(createAction(REMEMBER_REHYDRATED), (state, action) => {
      // "action.payload" is the Rehydrated Root State
      state.isRehydrated = true;
    })
    .addCase(createAction(REMEMBER_PERSISTED), (state, action) => {
      state.isPersisted = true;
    })
});

const reducers = {
  reduxRemember: reduxRemember.reducer,
  // ...
};

const rememberedKeys = [ 'myStateIsRemembered' ];
const reducer = rememberReducer(reducers);
const store = configureStore({
  reducer,
  enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(
    rememberEnhancer(
      window.localStorage, // or window.sessionStorage, or AsyncStorage, or your own custom storage driver
      rememberedKeys
    )
  )
});

export type RootState = ReturnType<typeof store.getState>;
export default store;

// Continue using the redux store as usual...

Usage - React rehydration gate

Preqrequisite: to be used with: Usage - inside a reducer or similar

import { FC, PropsWithChildren } from 'react';
import { useSelector } from 'react-redux';
import ReactDOM from 'react-dom/client';
import store, { RootState } from './store';

const RehydrateGate: FC<PropsWithChildren> = ({ children }) => {
  const isRehydrated = useSelector<RootState>((state) => state.reduxRemember.isRehyrdated);
  return isRehydrated
    ? children
    : <div>Rehydrating, please wait...</div>;
};

const root = ReactDOM.createRoot(
  document.getElementById('root')!
);

root.render(
  <Provider store={store}>
    <RehydrateGate>
      <App />
    </RehydrateGate>
  </Provider>
);

Usage - legacy apps (without redux toolkit)

Examples here are using redux toolkit. If your application still isn't migrated to redux toolkit, check the legacy usage documentation.

API reference

  • rememberReducer(reducers: Reducer | ReducersMapObject)

    • Arguments:
      1. reducers (required) - takes the result of combineReducers() function or list of non-combined reducers to combine internally (same as redux toolkit);
    • Returns - a new root reducer to use as first argument for the configureStore() (redux toolkit) or the createStore() (plain redux) function;
  • rememberEnhancer(driver: Driver, rememberedKeys: string[], options?: Options)

    • Arguments:
      1. driver (required) - storage driver instance, that implements the setItem(key, value) and getItem(key) functions;
      2. rememberedKeys (required) - an array of persistable keys - if an empty array is provided nothing will get persisted;
      3. options (optional) - plain object of extra options:
        • prefix: storage key prefix (default: '@@remember-');
        • serialize - a plain function that takes unserialized store state and its key (serialize(state, stateKey)) and returns serialized state to be persisted (default: JSON.stringify);
        • unserialize - a plain function that takes serialized persisted state and its key (serialize(state, stateKey)) and returns unserialized to be set in the store (default: JSON.parse);
        • persistThrottle - how much time should the persistence be throttled in milliseconds (default: 100)
        • persistDebounce (optional) - how much time should the persistence be debounced by in milliseconds. If provided, persistence will not be throttled, and the persistThrottle option will be ignored. The debounce is a simple trailing-edge-only debounce.
        • persistWholeStore - a boolean which specifies if the whole store should be persisted at once. Generally only use this if you're using your own storage driver which has gigabytes of storage limits. Don't use this when using window.localStorage, window.sessionStorage or AsyncStorage as their limits are quite small. When using this option, key won't be passed to serialize nor unserialize functions - (default: false);
        • errorHandler - an error handler hook function which is gets a first argument of type PersistError or RehydrateError - these include a full error stack trace pointing to the source of the error. If this option isn't specified the default behaviour is to log the error using console.warn() - (default: console.warn);
        • initActionType (optional) - a string which allows you to postpone the initialization of Redux Remember until an action with this type is dispatched to the store. This is used in special cases whenever you want to do something before state gets rehydrated and persisted automatically (e.g. preload your state from SSR). NOTE: With this option enabled Redux Remember will be completely disabled until dispatch({ type: YOUR_INIT_ACTION_TYPE_STRING }) is called;
    • Returns - an enhancer to be used with Redux