Sub topic 12
Control props for compound components
This chapter assumes you know the compound Tabs pattern with context and the controlled versus uncontrolled distinction. Here you combine them. The compound component keeps working on its own, and a parent can take over its state when needed.
The problem
A Tabs component that owns its selected value works until the consumer needs to know or decide which tab is active. Examples are saving the tab in the URL, blocking a switch while a form is invalid, or opening a tab from outside. Control props solve this. They are the props that let a parent supply the state (value) and receive changes (onValueChange).
Making the root controllable
Only the root component changes. Tab and Panel keep reading value and setValue from context, so they do not know whether the state is controlled.
import { createContext, useContext, useState } from 'react';
const TabsContext = createContext(null);
function useTabs() {
const ctx = useContext(TabsContext);
if (!ctx) {
throw new Error('Tabs subcomponents must be rendered inside <Tabs>');
}
return ctx;
}
export function Tabs({ value: controlledValue, defaultValue, onValueChange, children }) {
const isControlled = controlledValue !== undefined;
const [internalValue, setInternalValue] = useState(defaultValue);
const value = isControlled ? controlledValue : internalValue;
function setValue(next) {
if (!isControlled) {
setInternalValue(next);
}
onValueChange?.(next);
}
return (
<TabsContext.Provider value={{ value, setValue }}>
<div>{children}</div>
</TabsContext.Provider>
);
}
function Tab({ value, children }) {
const { value: active, setValue } = useTabs();
return (
<button role="tab" aria-selected={active === value} onClick={() => setValue(value)}>
{children}
</button>
);
}
function Panel({ value, children }) {
const { value: active } = useTabs();
return active === value ? <div role="tabpanel">{children}</div> : null;
}
Tabs.Tab = Tab;
Tabs.Panel = Panel;The context value is the single place where the choice is made. When the parent passes value, the internal state is ignored. The component only reports the request through onValueChange.
Using it in both modes
import { useState } from 'react';
import { Tabs } from './Tabs';
export function Example({ formIsValid }) {
const [tab, setTab] = useState('one');
function handleChange(next) {
if (next === 'two' && !formIsValid) return;
setTab(next);
}
return (
<Tabs value={tab} onValueChange={handleChange}>
<div role="tablist">
<Tabs.Tab value="one">One</Tabs.Tab>
<Tabs.Tab value="two">Two</Tabs.Tab>
</div>
<Tabs.Panel value="one">First panel</Tabs.Panel>
<Tabs.Panel value="two">Second panel</Tabs.Panel>
</Tabs>
);
}Here the parent refuses the change by not updating tab. The tab stays on "one" because the root keeps displaying the value it was given. Without value, <Tabs defaultValue="one"> behaves as before.
Common errors
- Passing
valuewithoutonValueChange. The tabs become unclickable because nothing updates the value. - Calling
onValueChangeonly in uncontrolled mode. Parents then never hear about changes. - Putting the controlled check in
Tab. Keep it in the root so all subcomponents behave the same way.
Exercise
A parent renders <Tabs value="one" onValueChange={() => {}}>. A user clicks the second Tab. Which tab panel is shown, and why?
Show answer
The first panel stays shown. The root is controlled because value is defined, so it uses that value and skips its internal state. The click only calls onValueChange, and the empty handler never updates the parent's value.