React Props vs State, React State vs Props, React Props and State Differences, When to Use Props vs State, React Props and State Explained
When developing React applications, two fundamental concepts play a crucial role in managing data: Props and State. These concepts determine how data flows within components, impacting component reusability, performance, and maintainability.
Understanding the key differences between React props and state is essential for writing efficient and scalable React applications. In this article, we’ll explore:
By the end, you’ll have a clear understanding of when to use props and when to use state in your React components.
In React, props (short for properties) are read-only inputs that a parent component passes to a child component. Props allow data and event handlers to be passed down the component tree, enabling reusable and modular components.
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return <Greeting name="John" />;
}
Here, the Greeting
component receives a name
prop from the App
component and displays it.
State is a built-in object that allows a component to maintain and manage dynamic data. Unlike props, which are passed from parent to child, state is managed within a component and can be updated over time.
setState
function (in class components) or useState
hook (in functional components).import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
);
}
Here, the Counter
component maintains a count
state, which updates when the button is clicked.
Feature | Props | State |
---|---|---|
Definition | Data passed from parent to child | Data managed within a component |
Mutability | Immutable (read-only) | Mutable (can be changed) |
Ownership | Controlled by the parent component | Controlled by the component itself |
Usage | Used for passing data & event handlers | Used for managing local component data |
Reusability | Promotes reusability & modularity | Typically specific to a component |
Keep props pure—do not modify props inside the child component.
Use destructuring to make props more readable:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
Use defaultProps to provide default values:
Greeting.defaultProps = {
name: "Guest",
};
Keep state local and avoid lifting it unless necessary.
Use functional updates when updating state based on the previous value:
setCount((prevCount) => prevCount + 1);
Avoid unnecessary state—if a value can be derived from props, don’t store it in state.
Wrong approach:
function Greeting(props) {
props.name = "Alice"; // ❌ This is not allowed
return <h1>Hello, {props.name}!</h1>;
}
Solution: Use state if modification is needed within the component.
Wrong approach:
function Message({ text }) {
const [message, setMessage] = useState(text); // ❌ Unnecessary state
return <p>{message}</p>;
}
Solution: Directly use props unless local modification is needed.
Wrong approach:
function App() {
const [user, setUser] = useState({ name: "John", age: 25 });
function updateAge() {
user.age = 26; // ❌ Direct mutation
setUser(user);
}
return <button onClick={updateAge}>Update Age</button>;
}
Solution: Always create a new object when updating state:
setUser((prevUser) => ({ ...prevUser, age: 26 }));
Both props and state are essential for managing data in React applications. Understanding their differences and best use cases will help you build more efficient and scalable applications.