Active store after component unmounts #2495
-
Hi, I have question regarding pinia behaviour when "root" (page) component unmounts. I create store instance on top of the page component, like this
and passing down its data to child components through props. Everything works as expected, but when I switch to different page (router), store is not "destroyed, unmounted, cleared". It stays as it is with all its data. So when you come back to this page, it loads previous data, not initial data. If I remember correctly (it was almost year ago) all stores within component were "destroyed" when component unmounts and "installed" when you come back. You could see in devtools Now its not happening and I wonder, if It was like this all the time (so my memory doesnt serve well), if something changed meantime or its because Im using "setup" store, not "option" store. Thank you for clarification |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This seems unlikely, to stay alive is the whole point of the store. If you need them to go away with the component, they should probably not be a store but just a composable. This will of course not let you reuse the the same data across components since each composable call would recreate state but it sounds like you are passing props so it should be good. If you still need the global access, you can always manually dispose of the store in the root component const store = useStore()
onUnmounted(() => {
store.$dispose()
delete pinia.state.value[store.$id]
}) |
Beta Was this translation helpful? Give feedback.
This seems unlikely, to stay alive is the whole point of the store. If you need them to go away with the component, they should probably not be a store but just a composable. This will of course not let you reuse the the same data across components since each composable call would recreate state but it sounds like you are passing props so it should be good.
If you still need the global access, you can always manually dispose of the store in the root component