Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
zustand-vue
Advanced tools
Readme
🐻 State-management for Vue based on zustand.
A small, fast and scalable bearbones state-management solution using simplified flux
principles. Has a comfy API based on hooks
, isn't boilerplatey or opinionated.
If you have good ideas, Welcome to build together ! 👏👏👏
:::tip
:::
npm install zustand-vue # or yarn add zustand-vue
The created store is a hook
, you can put anything in it: basic variables, objects, functions, state must be updated immutably, set
function merges state to achieve state update.
import create from "zustand-vue";
const useBearStore = create((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
setBears: (val)=>set({ bears: value })
removeAllBears: () => set({ bears: 0 }),
}))
export default useBearStore
Get your target state based on the selector
and the component will re-render on state change。
:::caution
Store binds components are different in vue3
vs vue2
。
:::
setup
<template>
<div>store.bears: {{ bears }}</div>
</template>
<script setup>
import useBearStore from "./store";
const bears = useBearStore((state) => state.bears)
</script>
useBearStore
<template>
<div>store.bears: {{ bears }}</div>
</template>
<script>
import useBearStore from "./store";
export default {
data() {
return {
bears: useBearStore((state) => state.bears),
};
}
};
</script>
setup
<script setup lang="ts">
import useBearStore from "./store";
const increasePopulation = useBearStore((state) => state.increasePopulation);
const removeAllBears = useBearStore((state) => state.removeAllBears);
</script>
<template>
<button @click="increasePopulation">increasePopulation</button>
<button @click="removeAllBears">removeAllBears</button>
</template>
store
initialize methods
<script>
import useBearStore from "./store";
const increasePopulation = useBearStore((state) => state.increasePopulation);
const removeAllBears = useBearStore((state) => state.removeAllBears);
export default {
methods: {
increasePopulation,
removeAllBears,
},
};
</script>
<template>
<button @click="increasePopulation">increasePopulation</button>
<button @click="removeAllBears">removeAllBears</button>
</template>
methods
call function<script>
import useBearStore from "./store";
const increase = useBearStore((state) => state.increasePopulation);
const remove = useBearStore((state) => state.removeAllBears);
export default {
methods: {
increasePopulation() {
increase();
},
removeAllBears() {
remove();
},
},
};
</script>
<template>
<button @click="increasePopulation">increasePopulation</button>
<button @click="removeAllBears">removeAllBears</button>
</template>
:::warning
In the vue2 environment, due to compatibility issues, selector
is not recommended. It is recommended to use useBearStore()
to get the state
:::
<template>
<div>store.bears: {{ Store.bears }}</div>
</template>
<script>
import useBearStore from "./store";
export default {
data() {
return {
Store: useBearStore(),
};
},
};
</script>
It can also be used with computed
<template>
<div>store.bears: {{ bears }}</div>
</template>
<script>
import useBearStore from "./store";
export default {
data() {
return {
Store: useBearStore(),
};
},
computed: {
bears() {
return this.store.bears;
},
},
};
</script>
store
initialize methods
<script>
import useBearStore from "./store";
const increasePopulation = useBearStore((state) => state.increasePopulation);
const removeAllBears = useBearStore((state) => state.removeAllBears);
export default {
methods: {
increasePopulation,
removeAllBears,
},
};
</script>
<template>
<button @click="increasePopulation">increasePopulation</button>
<button @click="removeAllBears">removeAllBears</button>
</template>
methods
call function<script>
import useBearStore from "./store";
const increase = useBearStore((state) => state.increasePopulation);
const remove = useBearStore((state) => state.removeAllBears);
export default {
methods: {
increasePopulation() {
increase();
},
removeAllBears() {
remove();
},
},
};
</script>
<template>
<button @click="increasePopulation">increasePopulation</button>
<button @click="removeAllBears">removeAllBears</button>
</template>
:::caution
Since zustand-vue follows the flux
model, its state has the feature of immutable update
, when you bind Input(Form) components, v-model
syntactic sugar will be invalid, set
must be used to update state
, as follows Examples of different methods according to vue2 and vue3:
<template>
<input v-model="bears" @input="handleChange" />
{/* or <input :bind="bears" @input="handleChange" /> */}
</template>
<script setup>
import useBearStore from "./store";
const setBears = useBearStore((state) => state.setBears);
const handleChange = (e) => { setBears(e.target.value) }
</script>
<template>
<input v-model="bears" @input="handleChange" />
{/* or <input :bind="bears" @input="handleChange" /> */}
</template>
<script>
import useBearStore from "./store";
const setBears = useBearStore((state) => state.setBears);
export default {
data() {
return {
bears: useBearStore((state) => state.bears),
};
},
methods: {
handleChange(e){
setBears(e.target.value)
}
}
};
</script>
<template>
<input v-model="bears" />
</template>
<script>
import useBearStore from "./store";
const setBears = useBearStore((state) => state.setBears);
export default {
data() {
return {
store: useBearStore(),
};
},
computed:{
bears:{
get(){
return this.store.bears
},
set(val){
setBears(val)
}
}
}
};
</script>
<template>
<input v-model="store.bears" @input="handleChange" />
{/* or <input :bind="bears" @input="handleChange" /> */}
</template>
<script>
import useBearStore from "./store";
const setBears = useBearStore((state) => state.setBears);
export default {
data() {
return {
store: useBearStore(),
};
},
methods:{
handleChange(e){
setBears(e.target.value)
}
}
};
</script>
if you want cross-application and cross-framework(react/vue) state management and sharing capabilities, maybe you can try zustand-pub.
FAQs
State management for vue based on zustand
The npm package zustand-vue receives a total of 78 weekly downloads. As such, zustand-vue popularity was classified as not popular.
We found that zustand-vue demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.