Skip to content

⬅️ Back to Table of Contents

πŸ“„ createInjectionState

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 3
πŸ“¦ Imports 3
πŸ“ Interfaces 1
πŸ“‘ Type Aliases 1

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/shared/createInjectionState/index.ts

πŸ“¦ Imports

Name Source
InjectionKey vue
injectLocal ../injectLocal
provideLocal ../provideLocal

Functions

createInjectionState(composable: (...args: Arguments) => Return, options: { defaultValue: Return } & CreateInject…): CreateInjectionStateReturn<Arguments, Return, Return>

Create global state that can be injected into components.

See: https://vueuse.org/createInjectionState

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Create global state that can be injected into components.
 *
 * @see https://vueuse.org/createInjectionState
 *
 * @__NO_SIDE_EFFECTS__
 */
Code
export function createInjectionState<Arguments extends Array<any>, Return>(
  composable: (...args: Arguments) => Return,
  options: { defaultValue: Return } & CreateInjectionStateOptions<Return>,
): CreateInjectionStateReturn<Arguments, Return, Return>

Internal helpers

Declared inside another function in this file.

useProvidingState(args: Arguments): Return

Parameters:

  • args Arguments

Returns: Return

Calls:

  • composable
  • provideLocal (from ../provideLocal)
Code
(...args: Arguments) => {
    const state = composable(...args)
    provideLocal(key, state)
    return state
  }

useInjectedState(): any

Returns: any

Calls:

  • injectLocal (from ../injectLocal)
Code
() => injectLocal(key, defaultValue)

Interfaces

CreateInjectionStateOptions<Return>

Interface Code
export interface CreateInjectionStateOptions<Return> {
  /**
   * Custom injectionKey for InjectionState
   */
  injectionKey?: string | InjectionKey<Return>
  /**
   * Default value for the InjectionState
   */
  defaultValue?: Return
}

Properties

Name Type Optional Description
injectionKey string \| InjectionKey<Return> βœ“ not shown
defaultValue Return βœ“ not shown

Type Aliases

CreateInjectionStateReturn<Arguments extends Array<any>, ProvideReturn, InjectReturn>

type CreateInjectionStateReturn<Arguments extends Array<any>, ProvideReturn, InjectReturn> = Readonly<[
  /**
   * Call this function in a provider component to create and provide the state.
   *
   * @param args Arguments passed to the composable
   * @returns The state returned by the composable
   */
  useProvidingState: (...args: Arguments) => ProvideReturn,
  /**
   * Call this function in a consumer component to inject the state.
   *
   * @returns The injected state, or `undefined` if not provided and no default value was set.
   */
  useInjectedState: () => InjectReturn,
]>;

Generated by Syntax Scribe