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/createInjectionState.md
1---2category: State3---45# createInjectionState67Create global state that can be injected into components.89## Usage1011```ts twoslash include useCounterStore12// useCounterStore.ts13import { createInjectionState } from '@vueuse/core'14import { computed, shallowRef } from 'vue'1516const [useProvideCounterStore, useCounterStore] = createInjectionState((initialValue: number) => {17// state18const count = shallowRef(initialValue)1920// getters21const double = computed(() => count.value * 2)2223// actions24function increment() {25count.value++26}2728return { count, double, increment }29})3031export { useProvideCounterStore }3233// If you want to hide `useCounterStore` and wrap it in default value logic or throw error logic, please don't export `useCounterStore`34export { useCounterStore }3536export function useCounterStoreWithDefaultValue() {37return useCounterStore() ?? {38count: shallowRef(0),39double: shallowRef(0),40increment: () => {},41}42}4344export function useCounterStoreOrThrow() {45const counterStore = useCounterStore()46if (counterStore == null)47throw new Error('Please call `useProvideCounterStore` on the appropriate parent component')48return counterStore49}50```5152```vue53<!-- RootComponent.vue -->54<script setup lang="ts">55// @filename: useCounterStore.ts56// @include: useCounterStore57// ---cut---58import { useProvideCounterStore } from './useCounterStore'5960useProvideCounterStore(0)61</script>6263<template>64<div>65<slot />66</div>67</template>68```6970```vue71<!-- CountComponent.vue -->72<script setup lang="ts">73// @filename: useCounterStore.ts74// @include: useCounterStore75// ---cut---76import { useCounterStore } from './useCounterStore'7778// use non-null assertion operator to ignore the case that store is not provided.79const { count, double } = useCounterStore()!80// if you want to allow component to working without providing store, you can use follow code instead:81// const { count, double } = useCounterStore() ?? { count: shallowRef(0), double: shallowRef(0) }82// also, you can use another hook to provide default value83// const { count, double } = useCounterStoreWithDefaultValue()84// or throw error85// const { count, double } = useCounterStoreOrThrow()86</script>8788<template>89<ul>90<li>91count: {{ count }}92</li>93<li>94double: {{ double }}95</li>96</ul>97</template>98```99100```vue101<!-- ButtonComponent.vue -->102<script setup lang="ts">103// @filename: useCounterStore.ts104// @include: useCounterStore105// ---cut---106import { useCounterStore } from './useCounterStore'107108// use non-null assertion operator to ignore the case that store is not provided.109const { increment } = useCounterStore()!110</script>111112<template>113<button @click="increment">114+115</button>116</template>117```118119## Provide a custom InjectionKey120121```ts122// useCounterStore.ts123import { createInjectionState } from '@vueuse/core'124import { computed, shallowRef } from 'vue'125126// custom injectionKey127const CounterStoreKey = 'counter-store'128129const [useProvideCounterStore, useCounterStore] = createInjectionState((initialValue: number) => {130// state131const count = shallowRef(initialValue)132133// getters134const double = computed(() => count.value * 2)135136// actions137function increment() {138count.value++139}140141return { count, double, increment }142}, { injectionKey: CounterStoreKey })143```144145## Provide a custom default value146147```ts148// useCounterStore.ts149import { createInjectionState } from '@vueuse/core'150import { computed, shallowRef } from 'vue'151152const [useProvideCounterStore, useCounterStore] = createInjectionState((initialValue: number) => {153// state154const count = shallowRef(initialValue)155156// getters157const double = computed(() => count.value * 2)158159// actions160function increment() {161count.value++162}163164return { count, double, increment }165}, { defaultValue: 0 })166```167168## Type Declarations169170```ts171export type CreateInjectionStateReturn<172Arguments extends Array<any>,173Return,174> = Readonly<175[176/**177* Call this function in a provider component to create and provide the state.178*179* @param args Arguments passed to the composable180* @returns The state returned by the composable181*/182useProvidingState: (...args: Arguments) => Return,183/**184* Call this function in a consumer component to inject the state.185*186* @returns The injected state, or `undefined` if not provided and no default value was set.187*/188useInjectedState: () => Return | undefined,189]190>191export interface CreateInjectionStateOptions<Return> {192/**193* Custom injectionKey for InjectionState194*/195injectionKey?: string | InjectionKey<Return>196/**197* Default value for the InjectionState198*/199defaultValue?: Return200}201/**202* Create global state that can be injected into components.203*204* @see https://vueuse.org/createInjectionState205*206* @__NO_SIDE_EFFECTS__207*/208export declare function createInjectionState<209Arguments extends Array<any>,210Return,211>(212composable: (...args: Arguments) => Return,213options?: CreateInjectionStateOptions<Return>,214): CreateInjectionStateReturn<Arguments, Return>215```216