Redux-persist: Why is rehydration not working for me?

Created on 6 Jun 2016  路  8Comments  路  Source: rt2zz/redux-persist

Hi, thank you for the awesome library. I am having some issue using this for react native 0.26, the rehydration did not really modify my state. Here is the log for the rehydrate action

screenshot at jun 06 14-22-37

and this is how I implement this into my app:

screenshot at jun 06 14-23-07

Its probably caused by something I did, any help is appreciated.

Thank You

Most helpful comment

Most likely you are modifying state in your reducers, so autoRehydrate is skipping your reducers.

In order for autoRehydrate to work, you need dataReducer to by default return state unmodified.

switch action.type:
// ...
default: return state

Your other option is to not use autoRehydrate and instead handle the rehydrate manually:

switch action.type:
  case REHYDRATE: 
    return {...state, ...action.payload.dataReducer}
// ...

I wrote this on my phone so let me know if anything is broken or confusing

All 8 comments

Most likely you are modifying state in your reducers, so autoRehydrate is skipping your reducers.

In order for autoRehydrate to work, you need dataReducer to by default return state unmodified.

switch action.type:
// ...
default: return state

Your other option is to not use autoRehydrate and instead handle the rehydrate manually:

switch action.type:
  case REHYDRATE: 
    return {...state, ...action.payload.dataReducer}
// ...

I wrote this on my phone so let me know if anything is broken or confusing

Thanks for the quick reply, I do have dataReducer and return state unmodified by default. Could it be caused by something else? It does work when I use manual hydration.

actually upon further inspection I think your createStore call is wrong, It only takes 3 arguments so it should look more like:

let enhancer = autoRehydrate()(devtools())
createStoreWithMiddleware(rootReducer, initialState, enhancer)

Man, I am so careless. Thanks for all the help.

馃憤

This just helped me. My issue was I had autoRehydrate last in the enhancer chain, after devTools.

const configureStore = (initialState) => {
  const enhancer = compose(
    applyMiddleware(thunk, createLogger()),
    devTools(),    
    autoRehydrate(),
  );
  return createStore(rootReducer, initialState, enhancer);
};

Swapping to

const configureStore = (initialState) => {
  const enhancer = compose(
    applyMiddleware(thunk, createLogger()),
    autoRehydrate(),
    devTools(),
  );
  return createStore(rootReducer, initialState, enhancer);
};

Fixed for me.

Most likely you are modifying state in your reducers, so autoRehydrate is skipping your reducers.

In order for autoRehydrate to work, you need dataReducer to by default return state unmodified.

switch action.type:
// ...
default: return state

Your other option is to not use autoRehydrate and instead handle the rehydrate manually:

switch action.type:
  case REHYDRATE: 
    return {...state, ...action.payload.dataReducer}
// ...

I wrote this on my phone so let me know if anything is broken or confusing

You've saved my life. Just realized the default case of the reducer must be state

I had the same issue and realised that in my reducer I did this:

defaut:
return{...state}

instead of

default:
return state

Was this page helpful?
0 / 5 - 0 ratings