Skip to content
On this page

createGlobalState

Category
Last Changed
8 months ago

Keep states in the global scope to be reusable across Vue instances.

Demo

name: 'Banana'
color: 'Yellow'
size: 'Medium'

Usage

// store.js
import { createGlobalState, useStorage } from '@vueuse/core'

export const useGlobalState = createGlobalState(
  () => useStorage('vueuse-local-storage', 'initialValue'),
)
// store.js
import { createGlobalState, useStorage } from '@vueuse/core'

export const useGlobalState = createGlobalState(
  () => useStorage('vueuse-local-storage', 'initialValue'),
)
// component.js
import { useGlobalState } from './store'

export default defineComponent({
  setup() {
    const state = useGlobalState()
    return { state }
  },
})
// component.js
import { useGlobalState } from './store'

export default defineComponent({
  setup() {
    const state = useGlobalState()
    return { state }
  },
})
  • createSharedComposable

Type Declarations

export declare type CreateGlobalStateReturn<T> = () => T
/**
 * Keep states in the global scope to be reusable across Vue instances.
 *
 * @see https://vueuse.org/createGlobalState
 * @param stateFactory A factory function to create the state
 */
export declare function createGlobalState<T>(
  stateFactory: () => T
): CreateGlobalStateReturn<T>
export declare type CreateGlobalStateReturn<T> = () => T
/**
 * Keep states in the global scope to be reusable across Vue instances.
 *
 * @see https://vueuse.org/createGlobalState
 * @param stateFactory A factory function to create the state
 */
export declare function createGlobalState<T>(
  stateFactory: () => T
): CreateGlobalStateReturn<T>

Source

SourceDemoDocs

Contributors

Anthony Fu
Preston Alvarado
jelf

Changelog

v4.11.2 on 5/30/2021
68c7d - feat(typedef): add return typedefs (#543) (#544)
createGlobalState has loaded