
ReactJS
Props are how React components communicate with each other. They're the way you pass information from one component to anotherβlike function parameters, but for components.
Props are properties you pass to a component, like function arguments. They let you customize how a component behaves so you can reuse it in different contexts without duplicating code.
Here's a simple example: imagine you want to display greetings to different users. Instead of creating separate components for each person, you create one component that accepts a name as a prop:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
<Greeting name="Alice" /> // Outputs: Hello, Alice!
<Greeting name="Bob" /> // Outputs: Hello, Bob!<details>
<summary>π More Examples</summary>
// Function parameters work similarly
function greet(name) {
return `Hello, ${name}!`;
}
greet("Alice"); // Hello, Alice!
// React props use the same concept
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
<Greeting name="Alice" /> // Hello, Alice!</details>
An important rule: you cannot change props inside a component. Props are read-only. This is a fundamental React principle that keeps your code predictable.
function Card({ title }) {
// β Wrong - don't do this!
// title = "New Title";
// β
Correct - just use the prop
return <div>{title}</div>;
}Instead of creating separate `SaveButton`, `DeleteButton`, and `EditButton` components, you create one reusable Button component that accepts props:
function Button({ text, color }) {
return <button style={{ background: color }}>{text}</button>;
}
// Now use it many ways without duplicating code
<Button text="Save" color="green" />
<Button text="Delete" color="red" />
<Button text="Edit" color="blue" />| Aspect | Props | State |
|---|---|---|
| Source | Passed from parent | Owned by component |
| Immutable | Yes β | Can change β |
| Purpose | Customize component | Store component data |
| Flow | Parent β Child | Within component |
Ready to practice? Challenges | Next: Passing Props to Components
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep β’ Build Real β’ Verify Skills β’ Launch Forward