An <input> can be used two ways. With value and onChange, the parent owns
the text: it's controlled. With defaultValue, the input keeps its own
text, and the parent only sets where it starts: it's uncontrolled. Good
reusable components offer the same choice.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Click stars in both ratings, then press Clear from the parent.
Reducers is uncontrolled: defaultValue={2}. The rating keeps its own
state, and the parent never needs to know.
Context is controlled: value={difficulty} and onChange={setDifficulty}.
The parent always knows the rating, and can change it, as the button does.
One hook for both
useControllableState decides which mode it's in by whether value was passed:
JavaScript
const isControlled = value !== undefined;const current = isControlled ? value : inner;
When the rating changes, it updates its own state only when uncontrolled,
and always tells the caller through onChange. Every component in a kit can
share this hook, so they all behave the same way:
JavaScript
function setValue(next) { if (!isControlled) setInner(next); onChange?.(next);}
Predict
Change the Context rating to value={difficulty} with noonChange.
Click a star. What happens?
Check your prediction
Nothing: the rating stays on 3. It's controlled, so it shows whatever the parent
says, and the parent never hears about the click. It's the same as an <input value> without onChange. React warns about that for inputs; your own
components can do the same.
Don't switch modes
A component shouldn't change from uncontrolled to controlled while it's on the
screen: if value goes from undefined to a number, the state it kept so far
is ignored. React warns about this for inputs too. Callers should pick one mode
and keep it, which usually means initialising state with a value, not
undefined.
Your turn
Exercise
Warn like React does
In useControllableState, log a warning when a component is controlled but has
no onChange: This component is controlled (it has a value) but has no
onChange, so it can't change. Log when that condition first becomes true,
not on every render. If the caller fixes the prop and later removes it again,
a new warning is useful. StrictMode may repeat the initial effect in development.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Two separate questions: when is the component in the broken state, and
when should the warning be logged? The first is a true/false fact you can
work out from the props on any render. The second is "when that fact becomes
true", which is about a change between renders, not about one render.
Hint 2: a first step
Work out the true/false fact first and log it during render, to check it: pass
value={3} with no onChange and click the stars a few times. The warning
repeats on every render, which is what React avoids. Which tool runs code
after a render only when a value it depends on has changed?
Show one solution
JavaScript
import { useEffect, useState } from "react";export function useControllableState({ value, defaultValue, onChange }) { const [inner, setInner] = useState(defaultValue); const isControlled = value !== undefined; const missingOnChange = isControlled && !onChange; useEffect(() => { if (missingOnChange) { console.warn("This component is controlled (it has a value) but has no onChange, so it can't change."); } }, [missingOnChange]); // ...rest unchanged}
Completion is saved in this browser only. It does not measure mastery.