
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
vue-state-composer
Advanced tools
Very lightweight, non-centralized, typesafe, hydratable state management based on the Composition Api.
⚠️ This project is experimental ⚠️
// @/stores/counter.ts
import { computed, toRefs } from '@vue/composition-api'
import { createStore } from 'vue-state-composer'
const counterStore = createStore({
name: 'Counter',
setup({ createState }) {
// createState works the same as `reactive`,
// but is needed vor DevTools support and hydration
const state = createState({
count: 0,
})
// use plain functions for "mutations" and "actions"
const modify = (mod: number) => (state.count += mod)
const increment = () => modify(1)
const decrement = () => modify(-1)
// use computed for "getters"
const absolute = computed(() => Math.abs(state.count))
// the setup function returns the stores API
// it works just like the setup function of a component
return {
...toRefs(state),
absolute,
modify,
increment,
decrement,
}
},
})
There are three ways to use a store:
counterStore.use(id?: string) to get a new (local) instance.counterStore.useProvider(id?: string) to get a new instance and automatically provide to child componentscounterStore.useConsumer() to get an instance previously provided by a parent componentThe id should be used as a unique identifier, if multiple instances of the same store may be used across the app. This allows to correctly hydrate after SSR and for better DevTools support.
// @/components/counter.vue
<template>
<div>
{{ count }}
<button @click="increment">Increment</button>
</div>
</template>
<script>
import counterStore from '@/stores/counter'
export default {
setup() {
const { count, increment } = counterStore.use()
return {
count,
increment,
}
},
}
</script>
// @/app.vue
import basketStore from '@/stores/counter'
export default {
setup() {
// using the returned API is optional
basketStore.useProvider()
},
}
// @/components/checkout.vue
<template>
<button @click="checkout" />
</template>
<script>
import basketStore from '@/stores/counter'
export default {
setup() {
const { checkout } = basketStore.useConsumer()
return {
checkout,
}
},
}
</script>
// main.ts
import { installDevtools } from 'vue-state-composer'
new Vue({
setup() {
// devTools will be removed from production build
// using only 0.5kb gzipped for SPA at the time of writing
if (process.env.NODE_ENV !== 'production') {
installDevtools()
}
},
})
// main.ts
import { provideComposer, createComposer } from 'vue-state-composer'
const composer = createComposer({
// inject hydration data
hydration: typeof window === 'undefined' ? {} : window.__COMPOSER_STATE__,
})
new Vue({
setup() {
// install composer in app
provideComposer(composer)
},
})
// entry-server.ts
router.onReady(() => {
context.rendered = () => {
// pass hydration data to SSR context
context.composerState = composer.exportHydrationData()
}
})
// index.html
<body>
// inject hydration data to window
{{{ renderState({ contextKey: 'composerState', windowKey: '__COMPOSER_STATE__' }) }}}
</body>
Copyright (c) 2020-present, Johannes Lamberts
FAQs
Composable state for Vue SPA and Universal Apps
We found that vue-state-composer demonstrated a not healthy version release cadence and project activity because the last version was released 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.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.