Skip to content

evgenyatlas/redux-local-save

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redux-local-save NPM version

Module for store state saving

Installation and use

$ npm install --save redux-local-save
import { createStore, applyMiddleware } from 'redux'
import createLocalSaveMiddleware, { loadStorage } from 'redux-local-save'

//Create middleware for installation.
const localSaveMiddleware = createLocalSaveMiddleware({
        //We specify actions which will start saving
        saveActions: [
            TEST_SAVE,
            //Debounce: each call defers saving to 300ms
            { type: TEST_SAVE_WITH_DEBOUNCE, debounce: 300 },
            //Throttle: call no more than 200ms
            { type: TEST_SAVE_WITH_THROTTLE, throttle: 200 }
        ]
    })

const store = createStore(
        reducer,
        //Load the saved state
        loadStorage(),
        applyMiddleware(localSaveMiddleware)
    )

If the store you have more than one?

Specify the storage name in options for createLocalSaveMiddleware and loadStorage

//userStore.js
...
const localSaveMiddleware = createLocalSaveMiddleware({
        //We specify actions which will start saving
        saveName: 'userStore'
    })
const store = createStore(
        reducer,
        loadStorage('userStore'),
        applyMiddleware(localSaveMiddleware)
    )
...
//animalStore.js
...
const localSaveMiddleware = createLocalSaveMiddleware({
        //We specify actions which will start saving
        saveName: 'animalStore'
    })
const store = createStore(
        reducer,
        loadStorage('animalStore'),
        applyMiddleware(localSaveMiddleware)
    )
...

Features

We specify what action will start saving

const localSaveMiddleware = createLocalSaveMiddleware({
        //there are two types of "saveAction"
        saveActions: [
            //1. DUMB
            SEND_CURRENT_WORD,
            //2. SMART (for example, added a parameter that defer saving (for 200 ms) after each action)
            {type: SEND_PREVIOUS_WORD, debounce: 200},
        ]
    })

DEBOUNCE AND THROTTLE

License

MIT © jeckyhit

About

module for locally saving state in localstorage

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published