createContext

Gitlab
A utility function for creating type-safe Vue context providers and consumers in Nuxt applications.
Added since: v0.1.0
Last changed:

Overview

createContext() is a utility that helps create type-safe context providers and consumers in Vue components. It provides a way to share data between components without prop drilling, similar to React's Context API.

Anatomy

Defining a context can only be done in a <script lang="ts"> tag, not in a <script setup lang="ts"> tag.
import { createContext } from '~/utils/createContext';

// Create a context with a provider component name
const [injectMyContext, provideMyContext]
    = createContext<MyContextType>('MyProvider');

// Or with multiple possible provider components
const [injectMyContext, provideMyContext]
    = createContext<MyContextType>(['ProviderA', 'ProviderB']);

// Optional: Specify a custom context name
const [injectMyContext, provideMyContext]
    = createContext<MyContextType>('MyProvider', 'CustomContextName');

API Reference

createContext()

Creates a context with type-safe provider and consumer functions.

PropDefaultType
providerComponentName*
string | string[]
The name(s) of the component(s) that will provide the context
contextName
string
Optional custom name for the context (used in error messages)
MethodType
injectContext
() => ContextValue
Consumes the context value from a provider component.
provideContext
(contextValue: ContextValue) => void
Provides a context value to child components.

injectContext()

Consumes the context value from a provider component. Returns the context value if found, otherwise returns the fallback value if provided.

Throws an error if the context is not found (component is not a child of the provider).

PropDefaultType
fallback
T
Optional fallback value to use if context injection fails

provideContext()

Provides a context value to child components.

PropDefaultType
contextValue*
T
The value to provide to child components

Example

<script lang="ts">
import { createContext } from '~/utils/createContext';

export type ThemeContext = {
  theme: 'light' | 'dark';
  toggleTheme: () => void;
}

export const [injectThemeContext, provideThemeContext]
    = createContext<ThemeContext>("ThemeProvider");
</script>

<script setup lang="ts">
const theme = ref<'light' | 'dark'>('light');
const toggleTheme = () => {
    theme.value = theme.value === 'light' ? 'dark' : 'light';
};

provideThemeContext({
    theme: theme.value,
    toggleTheme,
});
</script>

Type Safety

The utility is fully type-safe:

  • The context value type is preserved throughout the provider-consumer chain
  • TypeScript will enforce correct usage of the context value
  • Fallback values must match the context value type

Best Practices

  1. Always provide a fallback value when the context might not be available
  2. Use descriptive names for provider components to make error messages clear
  3. Keep context values minimal and focused on a single concern
  4. Consider using multiple contexts for different concerns rather than a single large context

Changelog