Skip to main content

createProvider

Callable

  • createProvider<T>(displayName: string, defaultValue?: null | T): readonly [hook: () => T, provider: React.Provider<T | null>, consumer: React.Consumer<T | null>]

  • Creates a context provider, hook, and consumer.

    @example
    interface FormContext {
    values: Record<string, string>
    onSubmit: (values: Record<string, string>) => void
    }

    const [useForm, FormProvider] = createProvider<FormContext>('Form')

    Type parameters

    • T

    Parameters

    • displayName: string

      The name of the context. Used for error messages.

    • defaultValue: null | T = null

      The default value of the context.

    Returns readonly [hook: () => T, provider: React.Provider<T | null>, consumer: React.Consumer<T | null>]

    A tuple of the hook, provider, and consumer.