Lifting State Up
Learn how to share state between React components by moving state to their nearest common parent, passing data through props, and updating shared state using callback functions.
1. What Is Lifting State Up?
Lifting State Up is a React technique in which state is moved from child components to their nearest common parent component. The parent manages the shared state and passes the required data to child components using props.
For example, at CIIT Training Institute, SAMADHAN and SAMIKSHA may need to display the same selected course. Instead of maintaining separate course states, their common parent can manage one shared state.
const [course, setCourse] = useState("React");
// Pass the same state value to both children
<Samadhan course={course} />
<Samiksha course={course} />
Both components receive the same course value from their parent.
2. Why Do We Need Lifting State Up?
Sometimes multiple components need the same information. If each component maintains its own independent state, updating one component will not automatically update the other.
course = "Java"
course = "React"
Each component maintains separate state, so the values can become different.
course = "Java"
course = "Java"
course = "Java"
Both children receive the same state value from the parent.
3. Example Without Lifting State Up
In this example, SAMADHAN and SAMIKSHA maintain separate course states. Changing SAMADHAN's course does not change SAMIKSHA's course.
import { useState } from "react";
function Samadhan() {
const [course, setCourse] = useState("React");
return (
<div>
<h3>SAMADHAN</h3>
<p>Course: {course}</p>
<button onClick={() => setCourse("Java")}>
Select Java
</button>
</div>
);
}
function Samiksha() {
const [course, setCourse] = useState("React");
return (
<div>
<h3>SAMIKSHA</h3>
<p>Course: {course}</p>
</div>
);
}
function App() {
return (
<div>
<Samadhan />
<Samiksha />
</div>
);
}
export default App;
Expected Browser Output
SAMADHAN
Course: React
SAMIKSHA
Course: React
4. Moving State to the Parent
We can solve the problem by moving the course state into
the common parent component, App. The parent
passes the course value to both children using props.
import { useState } from "react";
function Samadhan({ course }) {
return (
<div>
<h3>SAMADHAN</h3>
<p>Selected Course: {course}</p>
</div>
);
}
function Samiksha({ course }) {
return (
<div>
<h3>SAMIKSHA</h3>
<p>Selected Course: {course}</p>
</div>
);
}
function App() {
const [course, setCourse] = useState("React");
return (
<div>
<h2>CIIT Training Institute</h2>
<Samadhan course={course} />
<Samiksha course={course} />
</div>
);
}
export default App;
How Does It Work?
- The parent component creates state using
useState(). - The parent stores the selected course in
course. - The parent passes the value to SAMADHAN through props.
- The parent also passes the same value to SAMIKSHA.
- Both children display the same selected course.
CIIT Training Institute
SAMADHAN
Selected Course: React
SAMIKSHA
Selected Course: React
5. Updating Shared State Using Callback Functions
A child component cannot directly access a parent's local state. The parent can pass a callback function to the child. When the child calls that function, the parent updates its own state.
Parent passes state and functions down to children through props. A child calls a callback to request a state update in the parent.
import { useState } from "react";
function Samadhan({ course }) {
return (
<div>
<h3>SAMADHAN</h3>
<p>Selected Course: {course}</p>
</div>
);
}
function Samiksha({ onChangeCourse }) {
return (
<div>
<h3>SAMIKSHA</h3>
<button onClick={() => onChangeCourse("React")}>
Select React
</button>
<button onClick={() => onChangeCourse("Java")}>
Select Java
</button>
<button onClick={() => onChangeCourse("Testing")}>
Select Testing
</button>
</div>
);
}
function App() {
const [course, setCourse] = useState("React");
return (
<div>
<h2>CIIT Course Selection</h2>
<Samadhan course={course} />
<Samiksha
onChangeCourse={setCourse}
/>
</div>
);
}
export default App;
Expected Browser Behavior
Initially: SAMADHAN sees React.
Click Select Java: The parent state changes to Java.
After update: SAMADHAN sees Java.
SAMIKSHA's buttons remain available because the parent owns the selected course.
onChangeCourse("Java").
The parent provided setCourse as that callback,
so the parent's state is updated.
6. Lifting State Up with Multiple Components
Shared state can be used by more than two components. In this example, the parent manages the course, a selector changes it, and two display components show the updated value.
import { useState } from "react";
function CourseDisplay({ course }) {
return (
<div>
<h3>SAMADHAN</h3>
<p>Selected Course: {course}</p>
</div>
);
}
function CourseSelector({ onSelect }) {
return (
<div>
<button onClick={() => onSelect("React")}>
React
</button>
<button onClick={() => onSelect("Software Testing")}>
Software Testing
</button>
<button onClick={() => onSelect("Data Analytics")}>
Data Analytics
</button>
</div>
);
}
function CourseSummary({ course }) {
return (
<div>
<h3>SAMIKSHA</h3>
<p>Course Summary: {course}</p>
</div>
);
}
function App() {
const [course, setCourse] = useState("React");
return (
<div>
<h2>CIIT Training Institute</h2>
<CourseDisplay course={course} />
<CourseSelector onSelect={setCourse} />
<CourseSummary course={course} />
</div>
);
}
export default App;
Component Relationship
useState("React")
Displays course
Calls onSelect()
Displays course
7. Understanding State, Props, and Callbacks
| Concept | Purpose | Example |
|---|---|---|
| State | Stores the shared value in the parent. | useState("React") |
| Props | Passes data from parent to child. | course={course} |
| Callback | Lets a child request an update. | onSelect={setCourse} |
| State Setter | Requests an update to parent state. | setCourse("Java") |
8. Common Mistakes
Maintaining Separate State for Shared Data
If two components must display the same selected course, separate state variables can become inconsistent.
Solution: Keep the shared state in their nearest common parent.
Forgetting to Pass Props
Avoid:
<Samadhan />
Use:
<Samadhan course={course} />
Calling a Callback During Rendering
Avoid:
<button onClick={onSelect("Java")}>
Select Java
</button>
Use:
<button onClick={() => onSelect("Java")}>
Select Java
</button>
The second example passes a function that executes when the button is clicked.
Trying to Change Parent State Directly
A child should not try to access the parent's local state directly. The parent should provide a callback function to request changes.
9. Practice Questions
- What is Lifting State Up in React?
- Why do we move shared state to the nearest common parent?
- What problem can occur when two components maintain separate state?
- How does a parent pass state to child components?
- What is the role of a callback function?
- Create two components that display the same selected course.
- Update the selected course from a child using a callback.
- Explain the difference between state and props.
- Create a CIIT course selector with React, Software Testing, and Data Analytics.
Summary
Lifting State Up means moving shared state to the nearest common parent component. The parent manages the state using useState() and passes the required values to children through props. When a child needs to update shared state, it calls a callback function provided by the parent. React then updates the relevant components with the new value. This approach helps multiple components stay synchronized and makes state management easier to understand.