Todae

Todae

useControlledState

useControlledState(controlled, defaultValue, onChange) returns [value, change, latest] for a component that takes a value prop, a default and a change callback. While controlled is not undefined, it is the value, so the parent decides. Otherwise the hook keeps its own state, starting at defaultValue.

On this page
function Filter({ value, defaultValue = 'all', onValueChange }: FilterProps) {
  const [current, change] = useControlledState(value, defaultValue, onValueChange);
  return (
    <select aria-label="Status" value={current} onChange={(event) => change(event.currentTarget.value)}>
      <option value="all">All</option>
      <option value="open">Open</option>
    </select>
  );
}

change(next, ...args) builds on the latest write, not on the last render. It skips next when it equals that value (Object.is). Otherwise it stores next when uncontrolled, then calls onChange(next, ...args). So two writes before a render both count, and a toggle pressed twice toggles twice.

A controlled write renders the hook again. A change the parent hasn't passed back by then is dropped, and the next write builds on the value the parent passed. A controlled value with no onChange is read-only, so change does nothing.

Set open twice. The second press is already the latest value, so onChange does not run. Reset parent writes the controlled value from outside the hook.

import { Button, RadioGroup, Stack, Text, useControlledState } from '@tounsoo/todae';
import { useState } from 'react';

function Filter({
  label,
  value,
  defaultValue = 'all',
  onValueChange,
}: {
  label: string;
  value?: string | null;
  defaultValue?: string | null;
  onValueChange?: (next: string | null) => void;
}) {
  const [current, change] = useControlledState(value, defaultValue, onValueChange);
  return (
    <Stack.Root gap="condensed" align="start">
      <RadioGroup.Root aria-label={label} value={current} onValueChange={change}>
        <RadioGroup.Item value="all">All</RadioGroup.Item>
        <RadioGroup.Item value="open">Open</RadioGroup.Item>

Uncontrolled. Changes 0.

Controlled. Parent holds all. Changes 0.

Extra arguments, such as a reason, pass through to onChange: type them as the second type parameter, as in useControlledState<boolean, [reason: string]>(…). undefined means uncontrolled, so a controlled empty value is null. change takes a value, not an updater function. latest() reads the value the next write builds on. change keeps its identity while the hook stays controlled or uncontrolled, and reads the newest onChange when it runs. Keep one mode for a component's life: going from controlled to uncontrolled shows the hook's own state, not the last controlled value.

Todae's own parts, such as Checkbox and useCombobox, are built on this hook, so a component of yours answers these cases the same way.

When to use

When: A component of your own with a value prop, a default and a change callback.

When not: State no parent can set → useState.

Props

useControlledState props
NameTypeDescription
controlled RequiredT | undefined

The value prop. undefined means uncontrolled, so use null for an empty controlled value.

defaultValue RequiredT

Starting value when uncontrolled. Read on the first render only.

onChange(next: T, ...args: A) => void

Called with each new value and any extra arguments, such as a reason, controlled or not. Without it a controlled value is read-only.