Skip to main content
You are viewing Pre-Alpha documentation.
Vuetify0 Logo
Theme
Mode
Accessibility
Vuetify
Vuetify One

Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

createContext

The createContext factory function is at the heart of all functionality in Vuetify0. It is a small wrapper around the Vue 3 provide↗ and inject↗ APIs, allowing you to create a context that can be shared across components.


Advanced100% coverageFeb 4, 2026

Usage

ts
import { shallowRef } from 'vue'
import { createContext } from '@vuetify/v0'
import type { ShallowRef } from 'vue'

interface MyContext {
  isDisabled: ShallowRef<boolean>
  isSelected?: ShallowRef<boolean>
  type: 'primary' | 'secondary' | 'tertiary'
}

const [useContext, provideContext] = createContext<MyContext>('namespace')

provideContext({
  isDisabled: shallowRef(false) ,
  isSelected: shallowRef(true),
  type: 'primary',
})

export { useContext }

Architecture

createContext is the foundation for all dependency injection in Vuetify0:

Context Hierarchy

Use controls to zoom and pan. Click outside or press Escape to close.

Context Hierarchy

Examples

Notification System

This example demonstrates the provider/consumer pattern using createContext. The context acts as a contract between components that produce state and components that consume it.

Provider/Consumer Data Flow

Use controls to zoom and pan. Click outside or press Escape to close.

Provider/Consumer Data Flow

File breakdown:

FileRole
context.tsDefines the NotificationContext interface and creates the typed [useNotifications, provideNotifications] tuple
NotificationProvider.vueImplements the context—creates reactive state and methods, then calls provideNotifications()
NotificationConsumer.vueConsumes the context via useNotifications() to display and interact with notifications
notifications.vueEntry point that composes Provider around Consumer

Key patterns:

  • Provider components are invisible wrappers that render only <slot />

  • Consumers import only from context.ts, never from the Provider

  • shallowReactive for arrays that mutate in place

Click the buttons to add notifications. Click a notification to dismiss it.

No notifications. Click a button above to add one.

API Reference

The following API details are for the createContext composable.

Functions

useContext

(key: ContextKey<Z>, defaultValue?: Z | undefined) => Z

Injects a context provided by an ancestor component.

provideContext

(key: ContextKey<Z>, context: Z, app?: App<any> | undefined) => Z

Provides a context to all descendant components.

createContext

(keyOrOptions?: ContextKey<Z> | CreateContextOptions | undefined, defaultValue?: Z | undefined) => readonly [() => Z, (context: Z, app?: App) => Z] | readonly [(key: string, defaultValue?: Z) => Z, (key: string, context: Z, app?: App) => Z]

Options

suffix

string | undefined

Optional suffix to append to the runtime key

Was this page helpful?

© 2016-1970 Vuetify, LLC
Ctrl+/