useControllableState
State that can be controlled by a value prop or stored inside the component.
State that can be controlled by a value prop or stored inside the component.
Usage
'use client';
import { useControllableState } from '@prosazhin/pbcomponents';
function Example() {
const [open, setOpen] = useControllableState({ defaultValue: false });
return <button onClick={() => setOpen(!open)}>{open ? 'Open' : 'Closed'}</button>;
}Hook API
Parameters
| Parameter | Type / variants | Purpose | Default |
|---|---|---|---|
value? | T | External value for controlled state. Provide this or defaultValue. | — |
defaultValue? | T | Initial internal value. Provide this or value. | — |
onChange? | (value: T) => void | Called when the value changes. | — |
Returns
| Value | Type / variants | Purpose |
|---|---|---|
[0] value | T | undefined | Current value. Can be undefined when neither value nor defaultValue is provided. |
[1] setValue | (value: T) => void | Sets the value or reports a change through onChange. |