Skip to content

Instantly share code, notes, and snippets.

@DC3
Forked from Zikoat/1_vue2-zustand.md
Created August 1, 2024 18:36
Show Gist options
  • Save DC3/40738b3ab5cfe5615b83c96358389231 to your computer and use it in GitHub Desktop.
Save DC3/40738b3ab5cfe5615b83c96358389231 to your computer and use it in GitHub Desktop.

Revisions

  1. @Zikoat Zikoat revised this gist Apr 19, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1_vue2-zustand.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    ℹ️ This approach works for Vue 2.6 and above.

    🔍 Tiny: Just 10 lines of source code.
    🔍 Tiny: Just 10 lines of source code, no dependencies.

    ✨ The same Zustand store can be used with different libraries, such as Vue, React, or Svelte.

  2. @Zikoat Zikoat revised this gist Apr 19, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1_vue2-zustand.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How to Use Zustand with Vue 2
    # How to use Zustand with Vue 2

    💻 Check out this Codesandbox demo: [Vue 2 Playground](https://codesandbox.io/s/vue-2-playground-forked-q9r4x1?file=/src/bearStore.ts)

  3. @Zikoat Zikoat revised this gist Apr 19, 2023. No changes.
  4. @Zikoat Zikoat revised this gist Apr 19, 2023. No changes.
  5. @Zikoat Zikoat renamed this gist Apr 19, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @Zikoat Zikoat renamed this gist Apr 19, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @Zikoat Zikoat renamed this gist Apr 19, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @Zikoat Zikoat renamed this gist Apr 19, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @Zikoat Zikoat renamed this gist Apr 19, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @Zikoat Zikoat created this gist Apr 19, 2023.
    22 changes: 22 additions & 0 deletions BearPopulation.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <template>
    <div>
    Bear population: {{ bearStore.bears }}
    <br />
    <button @click="() => bearStore.increasePopulation()">
    Increase population
    </button>
    </div>
    </template>

    <script>
    import { bearStoreVue } from "./bearStore";
    export default {
    name: "BearPopulation",
    data() {
    return {
    bearStore: bearStoreVue,
    };
    },
    };
    </script>
    14 changes: 14 additions & 0 deletions bearStore.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import { createStore } from "zustand/vanilla";
    import { createFromStore } from "./vueZustand";

    type BearState = {
    bears: number;
    increasePopulation: () => void;
    };

    const bearStoreVanilla = createStore<BearState>((set) => ({
    bears: 0,
    increasePopulation: () => set((state) => ({ bears: state.bears + 1 }))
    }));

    export const bearStoreVue = createFromStore(bearStoreVanilla);
    26 changes: 26 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # How to Use Zustand with Vue 2

    💻 Check out this Codesandbox demo: [Vue 2 Playground](https://codesandbox.io/s/vue-2-playground-forked-q9r4x1?file=/src/bearStore.ts)

    ℹ️ This approach works for Vue 2.6 and above.

    🔍 Tiny: Just 10 lines of source code.

    ✨ The same Zustand store can be used with different libraries, such as Vue, React, or Svelte.

    ## Installation

    1. Copy `vueZustand.ts` to your project.

    ## Getting Started

    2. Create a vanilla Zustand store, like in `bearStore.ts`.
    3. Create your Vue store by passing the created vanilla Zustand store to `createFromStore`, as done in `bearStore.ts`

    ## Usage

    4. In the Vue component where you want to use the store, import the Vue store and use it. See `BearPopulation.vue`

    The Vue store you have created is reactive, and when the state in Zustand updates, your component will update accordingly.

    Vue's observable will only react to changes in the parts of your store that actually change, so there is no need to slice and check for equality as this is handled by Vue's observable. For more information, visit the [Vue observable documentation](https://v2.vuejs.org/v2/api/#Vue-observable).
    13 changes: 13 additions & 0 deletions vueZustand.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import Vue from "vue";
    import { StoreApi } from "zustand/vanilla";

    export const createFromStore = <T extends object>(store: StoreApi<T>) => {
    const state = store.getState();
    const observable = Vue.observable(state);

    store.subscribe((newState) => {
    Object.assign(observable, newState);
    });

    return observable;
    };