The paths option is nice, but sometimes I would like it to be the other way around and specify which paths should NOT persist.
paths: [/* ... */],
paths has this behaviour already: you choose what to include here. If some paths are not specified, they won't be stored. Do you think it should be better documented, @benface?
@gangsthub Sorry I guess I wasn鈥檛 clear. I know this already, but I would like a way to store all paths except a few that I specify. That way when I add new root-level keys to the store, they are automatically persisted, unless I add them to ignorePaths / excludePaths. Do you see what I mean?
Oh, I see what you mean. But maybe it could be "easily" added in user-land?
import { modules } from '@/store'
const modulesToOmit = ['moduleSomethingSomething']
const excludeModules = module => !modulesToOmit.includes(module)
const paths = Object.keys(modules).filter(excludeModules)
// Then use `paths` constant in `path` option...
What do you think?
Yes, sure could. Thanks for sharing that code. Feel free to close this if you think it鈥檚 not worth adding as an official API.
Let's leave that to @robinvdvleuten ;)
The example @gangsthub provided only excludes paths at a top-level. I think that a excludePaths boolean option alongside the paths array would work. The excludePaths option can be false by default, so any given path in paths are persisted. But when you pass true to excludePaths, all paths that are not in the paths option are persisted. Feel free to open a PR for this.
Hi, I like the idea of having another attribute to exclude paths or maybe a ! symbol?
I was wondering if it is possible to use the setItem function? but I am not sure how to import the default storage object.
Hi. In my case, I need to persist all keys, except two. As I couldn't find any documentation about it, I tried with the "reducer" function, like this:
plugins: [createPersistedState({
reducer: (state) => {
let reducer = Object.assign({}, state)
delete reducer.firstKeyToRemove
delete reducer.secondKeyToRemove
return (reducer)
}
})]
Where firstKeyToRemove and secondKeyToRemove are the ones to be excluded. It worked as expected, but it's certainly funny, because I understand the "reduce" function should be the opposite: "include only this keys".
Hi. In my case, I need to persist all keys, except two. As I couldn't find any documentation about it, I tried with the "reducer" function, like this:
A previous issue mentioned that there are some quirks with using the reducer in this way, especially if you use namespaces.
I agree that having a way to exclude specific paths would be helpful.
Most helpful comment
The example @gangsthub provided only excludes paths at a top-level. I think that a
excludePathsboolean option alongside thepathsarray would work. TheexcludePathsoption can be false by default, so any given path inpathsare persisted. But when you pass true toexcludePaths, all paths that are not in thepathsoption are persisted. Feel free to open a PR for this.