@posit-dev/shinyreact
    Preparing search index...

    Function useShinyInput

    • A React hook for managing a Shiny input value.

      This hook initializes a state variable with defaultValue and returns the current value and a function to update it, similar to React.useState.

      When the component mounts, it waits for Shiny to initialize. Once Shiny is initialized, this hook registers the input with the Shiny React registry and uses debounced updates to send values to the Shiny server via window.Shiny.setInputValue().

      The hook supports debouncing to optimize performance by batching rapid updates, and allows setting priority levels for input events.

      Note: This hook only sends data to Shiny. It does not automatically update the React state if the input is changed on the server-side (e.g., using updateTextInput()). For two-way binding, a custom Shiny input binding would be required.

      Type Parameters

      • T

      Parameters

      • id: string

        The ID that will be used for the Shiny input (input$<id>).

      • defaultValue: T

        The initial value for the input, used only on first mount (same semantics as React.useState's initial value). Subsequent renders may pass a different value, but it will be ignored. This means inline object/array literals like {} or [] are safe to pass — they won't cause unnecessary re-renders.

      • options: {
            debounceMs?: number;
            namespace?: string | null;
            priority?: EventPriority;
            type?: string;
        } = {}

        Optional configuration object.

        • OptionaldebounceMs?: number

          Debounce delay in milliseconds for input updates (default: 100).

        • Optionalnamespace?: string | null
        • Optionalpriority?: EventPriority

          Priority level for the input event (from Shiny's EventPriority enum).

        • Optionaltype?: string

          Optional input-handler name appended as ${id}:${type} when sending to Shiny. Use to route values through a Shiny input handler such as "shiny.datetime". Must be non-empty and contain no whitespace or :. The first mount of a given id finalizes the policy (an explicit type or the absence of one); a later mount that omits type is a no-op, but a later mount that supplies a type disagreeing with the finalized policy throws — the handler name is a server-side semantic and must be consistent across every useShinyInput / useSetShinyInput call for the same id.

      Returns [T, Dispatch<SetStateAction<T>>]

      A tuple containing the current value and a function to set the value: [value, setValue].