How to fully reset pinia(s)? #587
-
The problemI have dozens of pinia stores in my application, but I want to reset them in case when user is logging out. It is a headache to manually Possible solutionTo have an option to reset pinia completely to initial state. Is it good to fully reset global application state? I guess the answer is yes, because it is a fault-tolerant approach to design application that always can be resetted to it's initial state without pain. You know, initial state of any application is always the most stable state. This very-well-battle-tested approach comes from erlang/elixir world, but I don't sure that this is appropriate for such thing like front-end's global state management system. Also, there may be a case when some of stores should be resetted, but some not. Maybe globally reset only those stores, that are not used right now? Or provide callback like this: pinia.reset((store) => {
if (!store.id.startsWith('auth')) {
// yes, reset this store
return true
}
// oh no! don't touch auth store(s)!
return false
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can add this with a plugin: pinia.use(({ store, pinia }) => {
pinia.storesToReset = pinia.storesToReset || new Set()
pinia.storesToReset.add(store)
if (!pinia.reset) {
pinia.reset = () => {
pinia.storesToReset.forEach(store => store.$reset())
}
}
}) It can also be typed by extending |
Beta Was this translation helpful? Give feedback.
You can add this with a plugin:
It can also be typed by extending
Pinia
and you could also add a custom Option to skip resetting: https://pinia.esm.dev/core-concepts/plugins.html#adding-new-options