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

@paysera/redux-state-history

v1.1.0

Published

A simple library that provides means to save Redux state and revert it when needed

Downloads

4

Readme

@paysera/redux-state-history Travis CI Coverage Status

A simple library that provides means to save Redux state and revert it when needed.

Installation

  1. Require the package via package manager: npm i @paysera/redux-state-history

  2. Simply import the withHistory higher-order reducer and wrap your reducer with it. You may as well provide configuration as a second argument.

import { withHistory } from '@paysera/redux-state-history';
import { combineReducers } from 'redux';
import fooReducer from './foo';
import barReducer from './bar';
import bazReducer from './baz';

const config = {
    maxHistoryLength: 10,
};

const reducers = {
    foo: withHistory(fooReducer, config),
    bar: withHistory(barReducer),
    baz: bazReducer,
};

export default combineReducers(reducers);

Usage

redux-history is a simple collection of redux actions:

  • save()

Saves current branch into history.

import { save } from '@paysera/redux-state-history';
import fooAction from './fooAction';

function fooBar(payload) {
    return (dispatch) => {
        dispatch(save());
        dispatch(fooAction(payload));
    };
}

export default fooBar;

  • revert()

Reverts state a step back.

import { save, revert } from '@paysera/redux-state-history';
import fooAction from './fooAction';

function fooBar(payload) {
    return (dispatch) => {
        dispatch(save());
        dispatch(fooAction(payload));
        dispatch(revert());
    };
}

export default fooBar;

  • saveAndLock()

Saves and locks the history state, so you cannot save or revert it without unlocking.

import { saveAndLock, revert } from '@paysera/redux-state-history';
import fooAction from './fooAction';

function fooBar(payload) {
    return (dispatch) => {
        dispatch(saveAndLock());
        dispatch(fooAction(payload));
        
        try {
            dispatch(revert());
        } catch (error) {
            // catch HistoryLockError
        }
    };
}

export default fooBar;

  • revertAndUnlock()

Reverts and unlocks the history state.

import { saveAndLock, revertAndUnlock } from '@paysera/redux-state-history';
import fooAction from './fooAction';

function fooBar(payload) {
    return (dispatch) => {
        dispatch(saveAndLock());

        dispatch(fooAction(payload));

        dispatch(revertAndUnlock());
        
        dispatch(fooAction(payload));
        
        dispatch(saveAndLock());
    };
}

export default fooBar;

Tips

Dispatching redux-state-history actions from reducers

Although it is considered as an anti-pattern, there may be some use-cases where there is no other option as the current state is not valid anymore. For example, you want to revert your state to the previous saved state due to some error in the state (imagine you cannot make changes in seperate branch because you need to give feedback/render instantly to your user).

If you wanted to dispatch inside a reducer, you could do something like this:

import { dispatchAction, revert } from '@paysera/redux-state-history';
import store from './store';

export default (state, payload) => {
    dispatchAction(store, revert, payload);
    
    return state;
};

Or make a wrapper, that imports store and dispatchAction and exports a simple function which you can use later on.

Considerations

redux-state-history is scoped to the reducer it is being wrapped on, meaning you cannot manipulate states from within different reducers as different reducers have their own separate branches in the Redux state. Although, you may wrap as many reducers as you like using withHistory.

On the other hand, in case you need to access another reducer' state you could consider using only one reducer and do some state branching instead.