redux-persist-transform-filter icon indicating copy to clipboard operation
redux-persist-transform-filter copied to clipboard

Partial initial state and usage help / docs

Open damianobarbati opened this issue 7 years ago • 3 comments

@edy thanks for this repo! It seems the rehydration is not properly dealing with defaults when using transforms.

Here my default state:

const defaultState = {
    computing: [],
    notification: null,
    token: null,
    data: {},
};

Here the persist transforms applied (I want only main.token to be persisted):

import { createFilter, createWhitelistFilter, createBlacklistFilter } from 'redux-persist-transform-filter';

const loadFilter = createWhitelistFilter('main', ['token']);

const persistConfig = {
    key: 'user',
    keyPrefix: 'user/',
    transforms: [loadFilter]
};

What happens after rehydrate dispatch is the following:

{
  main: {
    token: 'blabla'
  },
  _persist: {
    version: -1,
    rehydrated: true
  }
}

Since data:{} default is not applied but totally removed because ignored in the transform I get errors.

How can I deal with this situation? Persist just wanted keys and apply defaults to the others? Would it be possible to always rehydrate to the default and then apply the rehydration of wanted subkeys?

damianobarbati avatar Feb 02 '18 21:02 damianobarbati

@edy Any hints on this? This prevents from using nested keys (a.b.c) as the loaded state isn't properly merged to the existing state, rather replaces it completely.

Edit This problem is actually caused by redux-persist only supporting (out of the box) 2 levels of auto merging. Implementing a deep merge state reconciler might be the way to go.

Related https://github.com/rt2zz/redux-persist/issues/491

erandagan avatar Jun 25 '18 06:06 erandagan

I was just looking into this for a new React Native app and after some debugging it looks like using the autoMergeLevel2 state reconciler fixes this issue for me. When using the autoMergeLevel1 state reconciler the whole reducer gets replaced as noted here https://github.com/rt2zz/redux-persist/blob/208c6b112ead87b3701dfacaae2cdbe78377775a/src/stateReconciler/autoMergeLevel1.js#L33 which causes it to blow the initial state up. autoMergeLevel2 merges it instead of only using the persisted state

gabceb avatar Sep 25 '18 16:09 gabceb

https://github.com/edy/redux-persist-transform-filter/issues/24

jasan-s avatar May 27 '19 03:05 jasan-s