Search code examples
javascriptreactjsreduxredux-formredux-persist

Persisting only some fields from the Reducer


Hi in our app we have a reducer pattern:

{
   data: //here we have our actual data
   fetching: // is it still fetching 
   intact: // has it been 'touched'
   error: //errors if need be
}

furthermore due to business demand I need to persist a ReduxForm which is its own can of worms...

form: {
    Foobar: {
      values: {

      },
      initial: {

      },
      syncErrors: {

      },
      registeredFields: {

      }
    }
  }
}

it is as you may have figured out is pointless to persist anything but data, but Redux-Persist persists the entire reducer. The examples on filtering and transformation is a bit... lackluster in my feel and I have been strugling to implement. Looking for an example


Solution

  • Ok so this works using redux-persist-transform-filter like @NickHTTPS sugested:

    import createFilter from 'redux-persist-transform-filter';
    
    const saveSubsetFilter = createFilter('form', ['Foo.bar']);
    
    const persistConfig = {
        key: 'form',
        storage,
        whitelist: ['form'],
        transforms: [saveSubsetFilter]
    };
    
    persistCombineReducers(persistConfig, { form: formReducer });
    

    works like a charm :)