Vuex-persistedstate: Add option to include or exclude paths

Created on 28 Nov 2016  路  6Comments  路  Source: robinvdvleuten/vuex-persistedstate

Sometimes it's easier to exclude certain paths than to manually include a bunch. If you're busy I can take a crack at this and file a PR.

enhancement

Most helpful comment

Btw, if anyone uses vuex with namespaces and not to persist some key in any module, Object.assign is not enough. You have to deep clone your state.

All 6 comments

Good suggestion @syropian! It's added in the v1.1.0 release. Please see the README for the _reducer_ function.

@robinvdvleuten Hello! Thanks for the cool plugin. Can you please elaborate on how this can work? An example would be excellent.

Hi @RashadSaleh! You can see how the functionality works by looking at the defaultReducer in plugin.js. It's in its base just a simple JS reducer which returns an object containing all values you want to store.

@robinvdvleuten
Is this the best approach for reducer function?

reducer: (state,paths) => {
                let reducer = Object.assign({},state);
                delete reducer.dates; // state which I don't want to persist.

                return reducer;
            }

Thanks in advance.

@Edujugon please checkout the default reducer method here. It should look something like this in (in your case);

reducer (state, paths) {
  // No need to use let as the reducer itself can be immutable which do not mean that the properties 
  are not mutable (https://ponyfoo.com/articles/var-let-const)
  const reducer = Object.assign({}, state);
  // state which you don't want to persist.
  delete reducer.dates;

  return reducer;
}

Much easier is it though to give an array of paths to persist as option (https://github.com/robinvdvleuten/vuex-persistedstate#createpersistedstateoptions)

Btw, if anyone uses vuex with namespaces and not to persist some key in any module, Object.assign is not enough. You have to deep clone your state.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylewelsby picture kylewelsby  路  5Comments

jerometremblay picture jerometremblay  路  6Comments

chadwtaylor picture chadwtaylor  路  5Comments

umardraz picture umardraz  路  3Comments

irrg picture irrg  路  6Comments