pinia-class-component icon indicating copy to clipboard operation
pinia-class-component copied to clipboard

$reset not resetting the state

Open angelhdzdev opened this issue 1 year ago • 0 comments

Hello! I was experimenting adding $reset to a setup function store (Composition API style), and found about this lib in the process.

Tried it, and $reset is not resetting the state. 🤔

My code:

@Store
class AuthStore extends Pinia {
  public isAuthenticated: boolean = false 
  public test: string = 'abc'

  public login(): void {

  }

  public logout(): void {

  }
}

let authStore: AuthStore | undefined
export const useAuthStore = () => {
// Had to do this or else I get a getActivePinia not ready error.
  if (!authStore) {
    authStore = new AuthStore()
  }
  return authStore
}

Then in pages/index.vue:

<script lang="ts" setup>
const authStore = useAuthStore()

async function handleResetButtonClick(): Promise<void> {
  authStore.$reset()
}

onMounted(() => {
  authStore.isAuthenticated = true
  authStore.test = 'def'
})
</script>

angelhdzdev avatar Jul 19 '23 06:07 angelhdzdev