Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/createGlobalState.md
1---2category: State3related: createSharedComposable4---56# createGlobalState78Keep states in the global scope to be reusable across Vue instances.910## Usage1112### Without Persistence (Store in Memory)1314```ts15// store.ts16import { createGlobalState } from '@vueuse/core'17import { shallowRef } from 'vue'1819export const useGlobalState = createGlobalState(20() => {21const count = shallowRef(0)22return { count }23}24)25```2627A bigger example:2829```ts30// store.ts31import { createGlobalState } from '@vueuse/core'32import { computed, shallowRef } from 'vue'3334export const useGlobalState = createGlobalState(35() => {36// state37const count = shallowRef(0)3839// getters40const doubleCount = computed(() => count.value * 2)4142// actions43function increment() {44count.value++45}4647return { count, doubleCount, increment }48}49)50```5152### With Persistence5354Store in `localStorage` with `useStorage`:5556```ts twoslash include store57// store.ts58import { createGlobalState, useStorage } from '@vueuse/core'5960export const useGlobalState = createGlobalState(61() => useStorage('vueuse-local-storage', 'initialValue'),62)63```6465```ts66// @filename: store.ts67// @include: store68// ---cut---69// component.ts70import { useGlobalState } from './store'7172export default defineComponent({73setup() {74const state = useGlobalState()75return { state }76},77})78```7980## Type Declarations8182```ts83export type CreateGlobalStateReturn<Fn extends AnyFn = AnyFn> = Fn84/**85* Keep states in the global scope to be reusable across Vue instances.86*87* @see https://vueuse.org/createGlobalState88* @param stateFactory A factory function to create the state89*90* @__NO_SIDE_EFFECTS__91*/92export declare function createGlobalState<Fn extends AnyFn>(93stateFactory: Fn,94): CreateGlobalStateReturn<Fn>95```96