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

$reset method doesn't reset object to the initial state #2090

Closed
loicdekester opened this issue Mar 21, 2023 · 2 comments
Closed

$reset method doesn't reset object to the initial state #2090

loicdekester opened this issue Mar 21, 2023 · 2 comments

Comments

@loicdekester
Copy link

Reproduction

https://stackblitz.com/edit/vue3-reset-pinia-store-qt5yn7

Steps to reproduce the bug

From the stackblitz above:
I created a "Setup" store with 3 varibales in my state:

const counter = ref(0);
const array = ref([]);
const data = ref({});

On the component you can increment the count add values to the array or change data's value.
Since $reset method is not available in a "Setup" store I used a plugin:

import cloneDeep from 'lodash.clonedeep'

export default function storeReset({ store }) {
  const initialState = cloneDeep(store.$state)
  store.$reset = () => store.$patch(cloneDeep(initialState))
} 

When I use the $reset the count goes back to 0 and the array is emptied but not the object.

When debugging the value of initialState is correct with data being an empty object. But the $patch doesn't actually change the value in the store.

Expected behavior

I expected the state of the object data to be an empty object on reset.

Actual behavior

$reset doesn't reset an objects value to the initial state

Additional information

No response

@fireharp
Copy link

After assigning data.value to an object it becomes part of state.

So min, max, and other parts of the assigned object will be a part of the state now.

function addData() {
  data.value ={
    min: 10,
    max: 17,
    data: [
      {
        1245: [{ a: 3, b: 2 }],
        2345: [{ a: 3, b: 2 }],
      },
    ],
  }
}

Then in your custom $reset function after you apply $patch of initialState which has data as {} it properly patches the store.data state with no updates.

In your reset plugin you may want to replicate the way it's done in pinia
https://github.com/vuejs/pinia/blob/v2/packages/pinia/src/store.ts#L329

Like this
https://stackblitz.com/edit/vue3-reset-pinia-store-bxm62e?file=src/store/plugins/storeReset.js

import cloneDeep from 'lodash.clonedeep'

export default function storeReset({ store }) {
  const initialState = cloneDeep(store.$state)
  store.$reset = () => {
    store.$patch($state => {
      Object.assign($state, initialState)
    })
  }
}

@loicdekester
Copy link
Author

I see, I didn't understand how $patch worked.
Thank you for the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants