Introduction
In React, props and state are two essential ways to store and manage data inside components. While they both hold information that can influence what is rendered on the screen, they serve different purposes and work in different ways.
Understanding the difference between them is essential for building interactive and maintainable React applications.
What are Props?
Props (short for properties) are read-only inputs that are passed from a parent component to a child component.
Think of props as function parameters; they let you pass data down the component tree.
Example
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
export default function App() {
return <Greeting name="Alice" />;
}
- Here, the name "Alice" is passed as a prop to the Greeting component.
- The child component cannot modify props; they are immutable.
What is the State?
State is a built-in data storage mechanism in a component that allows it to manage and update data over time.
Unlike props, state is mutable and can be updated within the component using the useState hook (for functional components) or this.setState (for class components).
Example
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, count is part of the component's state.
- Updating the state will cause the component to re-render with the new value.
Key Differences Between Props and State
Feature |
Props |
State |
Mutability |
Immutable (cannot be changed by the element) |
Mutable (can be changed within the component) |
Who Updates It? |
Parent component |
The component itself |
Purpose |
Pass data to child components |
Store and manage data that changes over time |
Accessibility |
Accessible via the props parameter |
Accessible via the useState hook or this.state |
Trigger Re-render? |
Yes, when the parent re-renders with new props |
Yes, when the state is updated |
How Props and State Work Together?
Props and state often work hand-in-hand.
- Props pass data into a component.
- The state manages data within the component.
Example
function Message({ text }) {
return <h2>{text}</h2>;
}
function App() {
const [message, setMessage] = useState("Hello!");
return (
<>
<Message text={message} />
<button onClick={() => setMessage("Updated Message!")}>
Change Message
</button>
</>
);
}
- The message is stored in the state of the App component.
- The Message component receives a message as a prop.
Common Mistakes to Avoid
- Trying to modify props inside a child component: This will not work.
- Overusing state: If data can be passed via props and doesn’t need to be managed locally, use props.
- Not initializing state properly: Always give state an initial value.
Summary
Props and state are both essential for handling data in React, but they serve different purposes. Props are read-only and passed from parent to child, while state is local and changeable within a component. Mastering when to use props, when to use state, and how they interact will make your React applications more efficient and easier to maintain.