Props and State are the two core ways React components receive and manage data. Props are read-only inputs from the outside, while state is mutable data owned by the component itself.
useState() or methods like setState().Props (short for “properties”) are read-only values passed from a parent component to a child component. They make components reusable and configurable.
// A simple component that receives a name via props and renders a greeting
function Welcome(props) {
return <h2>Hello, {props.name}!</h2>;
}
// Reusing the same component with different prop values
// Usage
<Welcome name="Aman" />
<Welcome name="Riya" />
State is local and mutable data owned by a component. When state changes, React automatically re-renders the component to reflect the new UI.
// State example: clicking the button updates local component state
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h3>Count: {count}</h3>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
setState() or useState().Props and state solve different problems. This table summarizes their differences:
| Aspect | Props | State |
|---|---|---|
| Ownership | Received from parent | Managed within the component |
| Mutability | Immutable | Mutable (via setState / useState) |
| Usage | Configure or pass data | Track and manage behavior |
| Trigger Re-render | When new props are received | When state is updated |
| Scope | Passed down the component tree | Local to the component |
Props and state often work together. Props define external data (like a user’s name), while state tracks dynamic changes (like the number of likes).
// Combining props (name) with local state (likes) inside a single component
import { useState } from "react";
function Profile({ name }) {
const [likes, setLikes] = useState(0);
return (
<div>
<h3>{name}</h3>
<p>Likes: {likes}</p>
<button onClick={() => setLikes(likes + 1)}>Like</button>
</div>
);
}
The name prop is passed from outside, while likes is internal state that changes as the user interacts.
// Using a function prop so the child can send data back to the parent
function Parent() {
const handleMessage = (msg) => alert(msg);
return <Child sendMessage={handleMessage} />;
}
function Child({ sendMessage }) {
return (
<button onClick={() => sendMessage("Hello Parent!")}>
Send Message
</button>
);
}
Here, the Child component calls sendMessage, a function it received as a prop, to communicate back to the Parent component.
If you render the Welcome component twice with different names:
<Welcome name="Aman" /> ➝ outputs Hello, Aman!<Welcome name="Riya" /> ➝ outputs Hello, Riya!When you click the button in the Counter or Profile components:
count or likes) increases.In the Parent and Child example, clicking the button in Child triggers an alert in the parent, demonstrating child-to-parent communication via function props.
state.count++); always use the setter function.Parent component that passes a name prop to a Child component.Child, use useState to track and display the number of likes.Goal: Master how props and state work together to manage data flow and interactivity in React components.