Back to Vueuse

createSharedComposable

packages/shared/createSharedComposable/index.md

14.3.0715 B
Original Source

createSharedComposable

Make a composable function usable with multiple Vue instances.

[!WARNING] When used in a SSR environment, createSharedComposable will automatically fallback to a non-shared version. This means every call will create a fresh instance in SSR to avoid cross-request state pollution.

Usage

ts
import { createSharedComposable, useMouse } from '@vueuse/core'

const useSharedMouse = createSharedComposable(useMouse)

// CompA.vue
const { x, y } = useSharedMouse()

// CompB.vue - will reuse the previous state and no new event listeners will be registered
const { x, y } = useSharedMouse()