Lifting state up
Code Lab40 min
Lifting state up
Two siblings need the same fact → the state moves to their parent, and the parent passes values down and setters as callbacks:
function CoursePage() {
const [openId, setOpenId] = useState(null);
return (
<>
<ChapterList openId={openId} onOpen={setOpenId} />
<LessonPanel lessonId={openId} />
</>
);
}This is the single most important structural skill in React. When a bug says "two components disagree", the fix is almost always: lift their shared state one level up. You will meet the same idea as "context" later — same principle, wider reach.
Exercise
Build an accordion: chapters list + a panel showing the selected lesson. Lift selectedId to the shared parent.
Practice this lesson in the IDE
Open the React practice workspace, edit the starter code, and run your work in the browser.
Open IDE and practice