rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.95k stars 865 forks source link

[v5] Question: localForage and Redux #583

Open n3ssi3 opened 6 years ago

n3ssi3 commented 6 years ago

Hi

I just stared using localForage for my app. I would like to store the loginState and some API returns to reduce the api calls and increase the page speed. It (kinda) works, but all the data is saved in one giant entry in my IndexDB: key: "persist:liveaboard" value: "{"meta":"{\"htmlTitle\":\"Indonesia Liveaboard\"}" .... "persist":"{\"version\":-1,\"rehydrated\":true}"}" (shortend version)_

Wouldn't it be nicer if the key would be the reducer? like this: key: "meta" value: "{\"htmlTitle\":\"Indonesia Liveaboard\"}"

What am I doing wrong?

Thank you

Here my code snippet:

const reducer = {
  meta: meta,
  login: login,
  loading: loading,
  cruises: cruises,
  ports: ports,
  destinations: destinations
}
localForage.config({
  name: 'Indonesia Liveaboard',
  storeName: 'liveaboard'
});

// state to be stored in localForage, but lets not persist someEmphemeralKey
const persistConfig = {
  key: 'liveaboard',
  storage: localForage,
}

let rootReducer = persistCombineReducers(persistConfig, reducer)

const store = createStore(
  rootReducer,
  {},
  compose(
    applyMiddleware(dataService),
  )
)

└── redux@3.7.2
└── redux-persist@5.4.0
├─┬ react-redux@5.0.6
rt2zz commented 6 years ago

redux persist v4 did it more or less how you describe. There were a few reasons this was changed to be a single entry in v5:

  1. get rid of non-deterministic keys (and the need for getAllKeys method in storage)
  2. eliminate the possibility for out of sync issues between different pieces of state
  3. most of the "work" is in serializing the state which we still do broken up per reducer, writing it to the db is very fast is async / optimized at the db level.
renthobo commented 5 years ago

Any Update on this one ? still getting a single entry in indexedb