Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Persisted state doesn't update when calling this.$reset() and then setting state values #52

Open
christianlmc opened this issue Mar 23, 2022 · 6 comments
Labels
🐛 bug Something isn't working 🪛 workaround There is a workaround for this issue

Comments

@christianlmc
Copy link

christianlmc commented Mar 23, 2022

Minimal example:

export const useStore = defineStore('main', {
  state: () => ({
    counter: 0,
  }),
  persist: true,
  actions: {
    // If this function is not async with await on $reset, the localstorage doesn't update (even though pinia does update)
    // If F5 is pressed after setCounter has been called, counter will go back to previous state instead of 100
    setCounter100() {
      this.$reset();
      this.counter = 100
    },
  },
})
@prazdevs
Copy link
Owner

Hi, what versions are you running ? I tried with the examples given in the repo (using vite + vue3) and with your exact store it works fine and properly saves counter as 100 in the storage, and rehydrates properly.

If you could provide a complete repo with your problem so it can be investigated. thanks :)

@prazdevs prazdevs added the ⚠️ invalid This issue does not fill requirements label Mar 25, 2022
@christianlmc
Copy link
Author

Sorry it took me so long, here is an example repo of the issue

https://github.com/christianlmc/bug-pinia-plugin-persistedstate

@prazdevs prazdevs added 🐛 bug Something isn't working and removed ⚠️ invalid This issue does not fill requirements labels Mar 28, 2022
@prazdevs
Copy link
Owner

Okay, i investigated a bit, and there seems to be some off behaviour with the $reset method. The way mutations are committed is not the same on the very first time, and the following ones.
If you put this in your HomeView.vue

<script setup lang="ts">
import { useCounterStore } from "@/stores/counter";
import { storeToRefs } from "pinia";

const counterStore = useCounterStore();
const { counter } = storeToRefs(counterStore);
const { setCounter100 } = counterStore;

counterStore.$subscribe((e) => { console.log(e) })


</script>

<template>
  <main>
    <button @click="setCounter100">do thing</button>
    <div style="font-size: 3rem">Counter</div>
    {{ counter }}
    <input v-model="counter" />
    <div>Look at Local Storage, on the 'Application' tab on the Inspector</div>
    <div>It shows {"counter":0} (Tested on Chrome 99)</div>
  </main>
</template>

After loading the page, the first click on the button will give you 2 subscription events, and the state will be store in localStorage properly, but if you click again, only 1 subscription event containing everything is emitted, and the state is not persisted correctly. Reloading the page resets this behaviour every time. This seems more like a pinia issue/behaviour im not aware of.

Also it seems suspiciously tied to this issue vuejs/pinia#1129

@prazdevs
Copy link
Owner

or this one vuejs/pinia#992

@christianlmc
Copy link
Author

Thank you @prazdevs, I will keep using the await this.$reset workaround for the time being

@prazdevs prazdevs added the 🪛 workaround There is a workaround for this issue label Apr 9, 2022
@prazdevs prazdevs changed the title Persisted state doesn't update when calling this.$reset() and then setting state values [core] Persisted state doesn't update when calling this.$reset() and then setting state values Sep 7, 2022
@ThatDeveloper
Copy link

Hey there @prazdevs
Is there an update on this matter? I can not make it work, even with the await. Could you maybe provide an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🪛 workaround There is a workaround for this issue
Projects
None yet
Development

No branches or pull requests

3 participants