From 737048f324c4deaf0ea02fa097360d05ee3eb8a9 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Wed, 3 May 2017 14:15:45 -0400 Subject: [PATCH] Clarify that an existing reducer is needed Fixes #9 --- README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3561439..cfd8b2a 100644 --- a/README.md +++ b/README.md @@ -4,26 +4,38 @@ Migrate redux state between versions with redux-persist. #### Usage ```js -import { compose, createStore } from 'redux' +import { compose, createStore, combineReducers } from 'redux' import { persistStore, autoRehydrate } from 'redux-persist' import createMigration from 'redux-persist-migrate' +// VERSION_REDUCER_KEY is the key of the reducer you want to store the state version in. +// You _must_ create this reducer, redux-persist-migrate will not create it for you. +// In this example after migrations run, `state.app.version` will equal `2` +const VERSION_REDUCER_KEY = 'app' + +// This is a list of changes to make to the state being rehydrated. +// The keys must be integers, and migrations will be performed in ascending key order. +// Note: blacklisted reducers will not be present in this state. const manifest = { - 1: (state) => ({...state, staleReducer: undefined}) - 2: (state) => ({...state, app: {...state.app, staleKey: undefined}}) + 1: (state) => ({...state, staleReducer: undefined}) + 2: (state) => ({...state, app: {...state.app, staleKey: undefined}}) } -// reducerKey is the key of the reducer you want to store the state version in -// in this example after migrations run `state.app.version` will equal `2` -let reducerKey = 'app' -const migration = createMigration(manifest, reducerKey) +const migration = createMigration(manifest, VERSION_REDUCER_KEY) const enhancer = compose(migration, autoRehydrate()) +const reducer = combineReducers({ + [VERSION_REDUCER_KEY]: (state = {}) => state, // This reducer will be used to store the version + otherReducer1, + otherReducer2, + // ... +}) + const store = createStore(reducer, null, enhancer) persistStore(store) ``` -In the above example `migration = createMigration(manifest, 'app')` is equivalent to the more generalized syntax: +In the above example `migration = createMigration(manifest, VERSION_REDUCER_KEY)` is equivalent to the more generalized syntax: ```js // alternatively with version selector & setter const migration = createMigration(