ivantsov/redux-webext

I'd like to persist the redux state with `redux-persist`, can you guide me how?

Closed this issue · 1 comments

I'd like to persist the redux state with `redux-persist`, can you guide me how?

You just need configure redux-persist to set and get values from the storage.local API, and make sure the serialize key is set to false in your persistConfig. (Make sure redux-persist version is >=5.4.0)

Here's the code that worked for me (https://github.com/reblws/tab-search/blob/master/src/core/reducers/services/storage-adapter.js):

const storage = browser.storage.local;

export default {
  getItem: key => storage.get(key).then(obj => obj[key]),
  setItem: (key, item) => storage.set({ [key]: item }),
  removeItem: key => storage.remove(key),
};

Persisting the root reducer (https://github.com/reblws/tab-search/blob/master/src/core/reducers/index.js):

const persistConfig = {
  key: 'root',
  storage, // The export defined above
  serialize: false,
  blacklist: ['state'],
};

Then invoke persistReducer and export it:

export default persistReducer(persistConfig, rootReducer);

After you create your store with the above persisted reducer, and call persistStore on it, pass that persisted store to createBackgroundStore as the named store argument.