Props vs State in React
Learn the difference between Props and State in React, how they work, and when to use each one in your components.
1. Introduction to Props vs State
Props and State are important concepts in React. Both help components work with data, but they have different purposes.
- Props: Data passed from a parent component to a child component.
- State: Data managed inside a component that can change over time.
// Props <Student name="Samadhan" /> // State const [count, setCount] = useState(0);
Props example: Samadhan
State example: Count = 0
2. What Are Props?
Props, short for properties, allow a parent component to send data to a child component. A child component should treat props as read-only and should not modify them directly.
function Student(props) {
return <h2>Student Name: {props.name}</h2>;
}
function App() {
return <Student name="Samiksha" />;
}
export default App;
Student Name: Samiksha
3. What Is State?
State is data managed within a component. When state changes using its setter function, React can render the component again to display the updated value.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
export default Counter;
Initially: Count: 0
After one click on Increase: Count: 1
After two clicks on Increase: Count: 2
4. Difference Between Props and State
The following table highlights the main differences between Props and State.
| Props | State |
|---|---|
| Passed from a parent component. | Managed within a component. |
| Read-only for the receiving component. | Updated using a state setter such as setCount. |
| Used to configure or provide data to a component. | Used to store changing component data. |
| Can change when the parent supplies different values. | Can change in response to user actions or other logic. |
| Example: student name. | Example: counter value. |
Props: Student Name = Pradnya
State: Counter Value = 0
After updating the counter: Counter Value = 1
5. Example of Props and State Together
A component can receive a student's name through props and maintain a separate counter using state.
import { useState } from "react";
function StudentCard({ name }) {
const [likes, setLikes] = useState(0);
return (
<div>
<h2>Student: {name}</h2>
<p>Likes: {likes}</p>
<button onClick={() => setLikes(likes + 1)}>
Like
</button>
</div>
);
}
function App() {
return <StudentCard name="Samadhan" />;
}
export default App;
Student: Samadhan
Likes: 0
After clicking Like once: Likes: 1
6. Can Props Change?
A child component must not directly modify its props. However, the parent can provide a different prop value when its own data changes. The child then receives the new value.
function Student({ name }) {
return <h2>Student: {name}</h2>;
}
function App() {
const studentName = "Samiksha";
return <Student name={studentName} />;
}
export default App;
Student: Samiksha
The Student component displays the value received through props.
7. How State Changes
Use the state setter to update state. React then renders the component with the new value. Do not directly modify state variables.
import { useState } from "react";
function StudentStatus() {
const [status, setStatus] = useState("Learning");
return (
<div>
<p>Status: {status}</p>
<button onClick={() => setStatus("Completed")}>
Complete Course
</button>
</div>
);
}
export default StudentStatus;
Initially: Status: Learning
After clicking Complete Course: Status: Completed
8. When Should You Use Props or State?
Use Props when:
- A parent needs to pass data to a child component.
- You want a reusable component with configurable values.
- You need to pass a callback function from a parent to a child.
Use State when:
- A component needs to remember a changing value.
- You need to handle input values, counters, or toggles.
- The displayed UI should respond to changes in component data.
// Props: configure the component <Student name="Pradnya" /> // State: manage a changing value const [isVisible, setIsVisible] = useState(true);
Props example: Student Name = Pradnya
State example: isVisible = true
When isVisible becomes false, conditional UI can be hidden.
9. Common Mistakes
- Trying to change props directly inside a child component.
- Changing state variables directly instead of using the setter.
- Using props when the component needs to manage its own changing data.
- Using state for values that can simply be calculated from existing props or state.
// Incorrect: do not modify props
function Student(props) {
// props.name = "New Name"; // Avoid this
return <p>{props.name}</p>;
}
// Correct: use a state setter
const [count, setCount] = useState(0);
setCount(count + 1);
The Student component displays the provided prop.
The counter updates through its state setter.
10. Quick Revision
Props pass data into components, while state stores data that a component manages and updates. Both can influence what React displays on the screen.
// Props <Student name="Samadhan" /> // State const [count, setCount] = useState(0);
Student Name: Samadhan
Initial Count: 0
Summary
Props are used to pass data from a parent component to a child component, and the receiving component should treat that data as read-only. State is managed within a component and can be updated with a state setter, allowing React to render the updated UI. Use props to configure reusable components and state to manage changing data such as counters, form inputs, and visibility. Understanding both concepts is essential for building interactive React applications.