# redux-session-manager-middleware

> Middleware for managing state in redux client-side apps

Latest version **2.1.0** (published 2017-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-session-manager-middleware
pnpm add redux-session-manager-middleware
yarn add redux-session-manager-middleware
bun add redux-session-manager-middleware
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2017-08-16 |
| First published | 2017-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.10.1 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steven Silvestri |
| Maintainers | ssilve1989 |
| Keywords | redux, sessionStorage, middleware, state management, redux-middlware, flux |

## Links

- npm: https://www.npmjs.com/package/redux-session-manager-middleware
- Repository: https://github.com/ssilve1989/redux-session-manager-middleware
- Homepage: https://github.com/ssilve1989/redux-session-manager-middleware#readme
- Issues: https://github.com/ssilve1989/redux-session-manager-middleware/issues
- npm.io page: https://npm.io/package/redux-session-manager-middleware

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2017-08-16
- 2.1.0-beta.0 (beta) — 2017-08-16
- 2.0.0 — 2017-08-16
- 1.0.1 — 2017-08-13
- 1.0.0 — 2017-08-13

## README

# Redux Session Manager Middleware
[![Travis](https://img.shields.io/travis/ssilve1989/redux-session-manager-middleware.svg)](https://travis-ci.org/ssilve1989/redux-session-manager-middleware)
[![npm](https://img.shields.io/npm/v/redux-session-manager-middleware.svg?style=flat-square)](https://www.npmjs.com/package/redux-session-manager-middleware)

This middleware will serialize your redux store to sessionStorage for your **client-side only** app, to allow you to populate the redux store with a preloaded state on refresh. *(SSR apps would handle this differently)*. How to restore the state is described [here](#restoring-state).

## Installation

```
npm i redux-session-manager-middleware
```

## Usage
Apply this middleware similarly to other redux middleware. The only difference
being that this middleware requires an options argument

```javascript
import { createStore } from 'redux';
import sessionManager from 'redux-session-manager-middleware'

const options = { 
  name : "myApplication",
  exclude : [
    "myReducer1",
    [
    	"myReducer2", [
    		'excludedProp1'
    		['keys', 'to', 'excluded', 'property2']
        ]
    ]
  ],
  ignoreActions: [ ACTION_1, ACTION_2 ]
};
const store = createStore(reducers, [ sessionManager(options)])
```

### ImmutableJS
If you are using ImmutableJS for your redux store, you can just change the import to
```javascript
import sessionManager from 'redux-session-manager-middleware/dist/immutable';
```
**IMPORTANT** This expects ImmutableJS v4.x+. Currently the 4.x branch is on release-candidate status. This branch
is needed because it provides `Map.deleteIn` that this middleware leverages.

### Options
| Property | Type | Required? | Description |
|:---|:---|:---|:---
name | string | yes | This will be the key in sessionStorage for the serialized state |
ignoreActions | array | no | An array of action types that will not execute this middleware |
exclude | array[string\|array] | no | An array containing either a string representing the reducer to exclude, or an array of [reducerName, keyPaths] where keyPaths can be either a string for a direct property of the reducer, or an array representing the keyPath to the property to be excluded.


### Restoring state

State restoration upon refresh, can be handled via middleware, but I'd recommend just populating the store with a preloadedState instead, so that unnecessary actions to achieve this result aren't needed.

This can simply be achieved by pulling out the state from sessionStorage and passing it to `createStore`

Ex:
```javascript
const restoredState = JSON.parse(sessionStorage.getItem('myReducer');
```
You may need to provide a `reviver` function to `JSON.parse` if you had custom classes. Documentation on `reviver` can be found [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)

### FAQ

**Q: My state isn't being saved after an action dispatches**

A: Since this is *middleware*, it is executed before the reducer receives the action to update the state. So this serialization is essentially 1 action behind. I've experimented with ugly hacks to get the modified state after the reducer but none that are worth shipping. If anyone has any ideas feel free to contribute!

---
_Source: https://npm.io/package/redux-session-manager-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
