Skip to content
GaudiGaudi Design System home

Search documentation

Search every page, heading and paragraph.

useControllableState

State that works controlled or uncontrolled, like native inputs.

HooksState@gaudi/design-system/hooks/state/use-controllable-stateView in StorybookSource

The component owns the state until its host passes value; then the host accepts, refuses or replaces each change offered through onChange. The setter takes a value or an updater and skips unchanged values. DataTable uses it.

Usage

import { useControllableState } from '@gaudi/design-system/hooks';
function Rating({ value, defaultValue = 0, onChange }) { const [rating, setRating] = useControllableState({ value, defaultValue, onChange });}

Examples

Uncontrolled

Storybook

Controlled, with the host refusing changes

Storybook

API

3 options
2 return values

Accessibility

Controlled components must still reflect every change visibly and to assistive technology (checked, expanded, selected), even when the host refuses it.

Notes

  • Decide once: switching between controlled and uncontrolled during a component life is not supported.