rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.96k stars 866 forks source link

Cannot modify the newState to add _persist when frozen. #747

Open hockeylagu opened 6 years ago

hockeylagu commented 6 years ago

I use immer to update my state and keep it immutable. Immer has the possibility to auto freeze the state to insure that we don't modify the state, but Redux-Perist modify it at line 153 of persistReducer

let newState = baseReducer(restState, action)
if (newState === restState) return state; else {
      newState._persist = _persist
      return conditionalUpdate(newState)
}

My unoptimal solution is to desactivate the freezing or revert the commit of this line to return a new State like that:

let newState = {
      ...baseReducer(restState, action),
     _persist,
}
return conditionalUpdate(newState)
BaggersIO commented 6 years ago

Same problem here ...

vanyathecyborg commented 6 years ago

Well, same problem, I will have to get rid of Immer then I guess

ColeMorton commented 6 years ago

It would be great if we could support Immer in the future.

mweststrate commented 6 years ago

Simple work around could be:

import { setAutoFreeze } from "immer"
setAutoFreeze(false)
lukasikora commented 5 years ago

@hockeylagu taking into account current version of persistStore, I mean states equality check:

if (newState === restState) return state

Your solution provide Immer support without errors and setAutoFreeze(false). Maybe it's not optimal solution but at least we modify already modified state without using Immer inside library.

 // run base reducer:
    // is state modified ? return original : return updated
    let newState = baseReducer(restState, action)
    if (newState === restState) return state
    else {
      newState = {
        ...newState,
        _persist: _persist
      }
      return conditionalUpdate(newState)
    }
Shawnxkwang commented 5 years ago

Is Immer supported now?

michaelcuneo commented 5 years ago

I also need this to work alongside Immer. I've tried every configuration that I can think of... I have opened up #1068

alexmcmillan commented 5 years ago

I too would very much appreciate being able to use these 2 great libraries together. It seems wrong to be calling setAutoFreeze(false) on an immutability library.

mweststrate commented 5 years ago

Without any further background knowledge, it could maybe be solved by using weakmap, rather than modifying the objects directly?

Op zo 15 sep. 2019 09:46 schreef Alex McMillan notifications@github.com:

I too would very much appreciate being able to use these 2 great libraries together. It seems wrong to be calling setAutoFreeze(false) on an immutability library.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rt2zz/redux-persist/issues/747?email_source=notifications&email_token=AAN4NBDZHROLQBJRJ4ETNELQJXY5FA5CNFSM4ETNXJO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6XMHAI#issuecomment-531547009, or mute the thread https://github.com/notifications/unsubscribe-auth/AAN4NBA2XYQE36X5Y5QESD3QJXY5FANCNFSM4ETNXJOQ .