redux-persist
Advanced tools
Comparing version 0.2.6 to 0.2.7
23
index.js
@@ -113,4 +113,6 @@ 'use strict' | ||
type: 'REHYDRATE', | ||
key: key, | ||
data: data, | ||
payload: { | ||
key: key, | ||
data: data, | ||
} | ||
} | ||
@@ -161,13 +163,18 @@ } | ||
function autoRehydrate(reducer){ | ||
function autoRehydrate(reducer, config){ | ||
let actionConstant = config.actionConstant || 'REHYDRATE' | ||
return function(state, action){ | ||
if(action.type === 'REHYDRATE'){ | ||
if(action.type === actionConstant){ | ||
let key = action.payload.key | ||
let data = action.payload.data | ||
var reducedState = reducer(state, action) | ||
if(state[action.key] !== reducedState[action.key]){ | ||
if(state[key] !== reducedState[key]){ | ||
return reducedState | ||
} | ||
var subState = {} | ||
for (var key in reducedState[action.key]) { subState[key] = reducedState[action.key][key] } | ||
for (var key in action.data) { subState[key] = action.data[key] } | ||
reducedState[action.key] = subState | ||
for (var subkey in reducedState[key]) { subState[subkey] = reducedState[key][subkey] } | ||
for (var subkey in data) { subState[subkey] = data[subkey] } | ||
reducedState[key] = subState | ||
return reducedState | ||
@@ -174,0 +181,0 @@ } |
{ | ||
"name": "redux-persist", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "persist and rehydrate redux stores", | ||
@@ -9,2 +9,15 @@ "main": "index.js", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/rt2zz/redux-persist.git" | ||
}, | ||
"homepage": "https://github.com/rt2zz/redux-persist", | ||
"keywords": [ | ||
"redux", | ||
"redux-middleware", | ||
"react-native", | ||
"flux", | ||
"persistent", | ||
"localstorage" | ||
], | ||
"author": "rt2zz <zack@root-two.com>", | ||
@@ -11,0 +24,0 @@ "license": "MIT", |
# Redux Persist | ||
Persist and rehydrate a redux store. | ||
Persist a redux store. | ||
This module is an early experiment. Feedback welcome. | ||
* Operates on a per reducer basis | ||
* Performant out of the box (uses a time interator and operates on state partials) | ||
* Knows as little as possible about your application state and reducer internals | ||
* Supports any storage backend including localStorage, react-native AsyncStorage, or any conforming api | ||
**v0.2.3** Try out the new autoRehydrate higher order reducer | ||
**NOTE** 0.2.7 switched to flux standard actions. If you are using custom rehydration be sure to update `action.key => action.payload.key` and `action.data => action.payload.data` | ||
Implementing rehydration is very application specific. Check out some [recipes](https://github.com/rt2zz/redux-persist/blob/master/docs/recipes.md). | ||
##Basic Usage | ||
Basic usage requires adding three lines to a traditional redux application: | ||
```js | ||
import { persistStore, autoRehydrate } from 'redux-persist' | ||
const reducer = autoRehydrate(combineReducers(reducers)) | ||
const store = createStoreWithMiddleware(reducer) | ||
const store = createStore(reducer) | ||
persistStore(store, {}, () => { | ||
console.log('restored') | ||
}) | ||
/** | ||
persist store will immediately begin reading from disk and dispatching | ||
rehydrate actions for each key in the store. autoRehydrate will handle | ||
these actions automatically. If you need a custom handler add it in your | ||
reducer roughly as follows: | ||
**/ | ||
persistStore(store) | ||
``` | ||
For more complex rehydration, add a handler to your reducer: | ||
```js | ||
case REHYDRATE: | ||
@@ -37,9 +35,15 @@ if(action.key === 'myReducer'){ | ||
//update something | ||
action.data.initializationTime = Date.getTime() | ||
//immutable data | ||
let someIndex = Immutable.List(action.data.someIndex) | ||
return {...state, ...action.data} | ||
return {...state, ...action.data, someIndex} | ||
} | ||
return state | ||
``` | ||
You may need to configure the persistance layer, or take action after rehydration has completed: | ||
```js | ||
persistStore(store, {blacklist: ['someTransientReducer']}, () => { | ||
store.dispatch({type: 'REHYDRATION_COMPLETE'}) | ||
}) | ||
``` | ||
@@ -55,2 +59,5 @@ ##API | ||
- `autoRehydrate(reducer)` | ||
- This is a higher order reducer that will automatically shallow merge the persisted state for each key. | ||
- `.purge(keys)` | ||
@@ -62,6 +69,9 @@ - **keys** *array* An array of keys to be purged from local storage. | ||
##React-Native | ||
## Storage Backends | ||
Use any storage backend including: **localStorage** (default), react-native **AsyncStorage**, or a conforming **custom** storage api. | ||
#### React-Native Example | ||
```js | ||
var { AsyncStorage } = require('react-native') | ||
var persistStore = require('redux-persist-store') | ||
var { persistStore } = require('redux-persist') | ||
@@ -73,3 +83,10 @@ persistStore(store, {storage: AsyncStorage}, () => { | ||
##Auto Rehydrate | ||
## Motivations & Explanations | ||
Conceptually redux-persist operates on a per reducer basis. This enables the persistance layer to know as little about the application as possible. This is important, reducers should be the single source of truth for your state manipulation. | ||
It also enables great out of the box performance, as each save only operates on chunks of state, rather than the entire state object. | ||
While auto rehydration works out of the box, individual reducers can opt in to handling their own rehydration, allowing for more complex operations like applying data transforms, or doing cache invalidation. Simply define a handler for the rehydrate action in your reducer, and if the state is mutated, auto rehydrate will skip that key. | ||
## Auto Rehydrate | ||
Auto rehydrate is a higher order reducer that automatically rehydrates state. If you have a reducer that needs to handle its own hydration, perhaps with special time expiration rules, simply add a rehydration handler in your reducer, and autoRehydrate will ignore that reducer's keyspace. | ||
@@ -81,9 +98,2 @@ | ||
#### Why might this be a terrible idea? | ||
- Not well tested | ||
- Short circuits the normal reducer for 'REHYDRATE' actions | ||
- Does not use ActionType Constant | ||
- Does not support custom ActionCreators | ||
- May not play well with other extensions like devtools | ||
##Implementation Notes | ||
@@ -90,0 +100,0 @@ For performance |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12064
5
166
2
98
0