Loading Depth Study
Preparing your workspace
Lifting state up means moving shared state to a common parent and passing the needed data and actions through props.
Sometimes multiple components need to use or change the same piece of information. Keeping separate copies of the state can cause them to become inconsistent. Lifting the shared state into a common parent creates one source of truth. The parent passes the current value to children and passes callback functions when children need to request changes. This pattern is simple and powerful, although deeply nested prop passing may suggest that another state-sharing approach could be useful.
A course page may have a lesson list and lesson viewer that both need to know which lesson is currently selected.
import { useState } from 'react';
function LessonList({ selectedId, onSelect }) {
return (
<button onClick={() => onSelect(2)}>
Select Lesson 2
</button>
);
}
export default function CoursePage() {
const [selectedId, setSelectedId] = useState(1);
return (
<LessonList
selectedId={selectedId}
onSelect={setSelectedId}
/>
);
}The parent owns the selected lesson state and provides the child with the current value and a way to change it.