Skip to content

Instantly share code, notes, and snippets.

@mgussekloo
Last active March 21, 2022 19:48
Show Gist options
  • Save mgussekloo/25e287eed70babc5b5a91ee8ab88a559 to your computer and use it in GitHub Desktop.
Save mgussekloo/25e287eed70babc5b5a91ee8ab88a559 to your computer and use it in GitHub Desktop.
Getting $reset in Pinia composition api
export const useLocationStore = defineStore('location', () => {
    const defineState = () => ({
        id: 1,
        backgroundImage: '',
    })

    const state = defineState();

    Object.keys(state).forEach((key) => {
        state[key] = ref(state[key]);
    });

    function $reset() {
        const originalState = defineState();
        Object.keys(originalState).forEach((key) => {
            state[key].value = originalState[key];
        });
    }

    return {
    ...state
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment