shadcn-ahooks

useControllableValue

In some components, we need the state to be managed by itself or controlled by it's parent. useControllableValue is a Hook that helps you manage this kind of state.

Overview

In some components, we need the state to be managed by itself or controlled by it's parent. useControllableValue is a Hook that helps you manage this kind of state.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useControllableValue.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useControllableValue.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useControllableValue.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useControllableValue.json

In some components, we need the state to be managed by itself or controlled by it's parent. useControllableValue is a Hook that helps you manage this kind of state.

Examples

Uncontrolled component

Controlled component

No value, have onChange component

API

const [state, setState] = useControllableValue(props: Record<string, any>, options?: Options);

Result

PropertyDescriptionType
stateState-
setStateSet state(value: any | ((prevState: any) => any)) => void

Params

PropertyDescriptionTypeDefault
propsComponent propsRecord<string, any>-
optionsOptional configurationOptions-

Options

PropertyDescriptionTypeDefault
defaultValueThe default value, will be overridden by props.defaultValue and props.value--
defaultValuePropNameCustom defaultValue attribute namestringdefaultValue
valuePropNameCustom value attribute namestringvalue
triggerCustom trigger attribute namestringonChange

On this page