Overview
- Single source of truth
- The parent
<PercentageController>keeps one piece of state:const [percentage, setPercentage] = useState(50);
- The parent
- Prop-drilling callbacks
- Each
<Slider>receives the currentvalueand anonChangehandler:<Slider label="Slider 1" value={percentage} onChange={setPercentage} testid="slider-1" /> - The slider calls
onChange(Number(e.target.value))on every drag, updating the parent state. Because both sliders read that same state, they stay perfectly in sync.
- Each
Key take-aways
- Lift shared state to the nearest common ancestor.
- Pass data down, send events up (unidirectional data flow).
- A single state update re-renders all dependants, keeping UI consistent.
import PercentageController from "./PercentageController"; export default function App() { return ( <div className="min-h-screen flex items-center justify-center bg-gray-50 p-6"> <PercentageController /> </div> ); }
JavaScript Console
console.log()statements will appear here
Tests