@carbon/react
Version:
React components for the Carbon Design System
35 lines (34 loc) • 1.32 kB
TypeScript
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
interface UseControllableStateConfig<T> {
/** The name of the component. */
name?: string;
/**
* The default value for the component. This value is used when the component
* is uncontrolled (i.e., when `value` is not provided).
*/
defaultValue: T;
/**
* This callback is called whenever the state changes. It's useful for
* communicating state updates to parent components of controlled components.
*/
onChange?: (value: T) => void;
/**
* Controlled value. If this prop is omitted, the state will be uncontrolled.
*/
value?: T;
}
/**
* This hook simplifies the behavior of a component that has state which can be
* both controlled and uncontrolled. It works like `useState`. You can use the
* `onChange` callback to communicate state updates to parent components.
*
* Note: This hook will warn if the component switches between controlled and
* uncontrolled states.
*/
export declare const useControllableState: <T>({ defaultValue, name, onChange, value, }: UseControllableStateConfig<T>) => [T, (stateOrUpdater: T | ((prev: T) => T)) => void, boolean];
export {};