-
-
Save DC3/40738b3ab5cfe5615b83c96358389231 to your computer and use it in GitHub Desktop.
Revisions
-
Zikoat revised this gist
Apr 19, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, no dependencies. ✨ The same Zustand store can be used with different libraries, such as Vue, React, or Svelte. -
Zikoat revised this gist
Apr 19, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # 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) -
Zikoat revised this gist
Apr 19, 2023 . No changes.There are no files selected for viewing
-
Zikoat revised this gist
Apr 19, 2023 . No changes.There are no files selected for viewing
-
Zikoat renamed this gist
Apr 19, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Zikoat renamed this gist
Apr 19, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Zikoat renamed this gist
Apr 19, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Zikoat renamed this gist
Apr 19, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Zikoat renamed this gist
Apr 19, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Zikoat created this gist
Apr 19, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; };