Use of Props
Use of Props
Props stands for properties.
They are used to pass data from a parent component to a child component.
๐ Props make components dynamic, reusable, and configurable.
๐น Parent โ Child Relationship
Props work in one direction only:
Parent Component โ Child ComponentExample
function Parent() {
return <Child name="Naveen" />;
}
function Child(props) {
return <h1>Hello {props.name}</h1>;
}โ Parent sends data
โ Child receives data
โ Child cannot change props
๐น Why Props Are Important
1๏ธโฃ Reusability
Same component, different data.
<User name="Naveen" />
<User name="Amit" />
<User name="Riya" />function User(props) {
return <p>User Name: {props.name}</p>;
}โ One component
โ Multiple uses
โ Less code
2๏ธโฃ Make Components Dynamic
Without props โ static
With props โ dynamic
<Button text="Login" />
<Button text="Register" />๐น Props are Read-Only (Very Important)
โ You cannot modify props inside the child component.
function Child(props) {
props.name = "New Name"; // โ NOT ALLOWED
}โ Props are immutable
โ React enforces one-way data flow
๐ If data must change โ use state in parent
๐น Passing Data Up (Child โ Parent)
Props themselves cannot change, but you can pass a function as a prop.
function Parent() {
const handleClick = () => {
console.log("Button clicked");
};
return <Child onClick={handleClick} />;
}
function Child(props) {
return <button onClick={props.onClick}>Click</button>;
}โ Data still flows one-way
โ Child communicates via callback
๐น Default Props (Correction Needed)
โ โprops are only set default propertiesโ โ Incorrect
โ
Props are values passed from parent
โ
Default props are used only when parent does not pass value
Using default parameters
function User({ name = "Guest" }) {
return <p>Hello {name}</p>;
}๐น Props with Destructuring (Best Practice)
function User({ name, age }) {
return (
<p>
{name} - {age}
</p>
);
}โ Cleaner
โ Readable
โ Professional
๐น Passing Multiple Data Types
<User
name="Naveen"
age={22}
isStudent={true}
skills={["React", "Node"]}
/>Props can pass:
String
Number
Boolean
Array
Object
Function
JSX
๐น Nested Components & props.children
Example
function Card(props) {
return <div className="card">{props.children}</div>;
}
function App() {
return (
<Card>
<h1>Hello</h1>
<p>This is inside Card</p>
</Card>
);
}โ Nested components
โ props.children renders inner content
โ Common in layouts, modals, wrappers
๐น Props vs State (Quick Comparison)
Feature | Props | State |
|---|---|---|
Used for | Data passing | Data handling |
Mutability | Read-only | Changeable |
Ownership | Parent | Component itself |
Re-render | On parent change | On state change |
๐น Common Interview Mistakes (Avoid These)
โ โProps can be changedโ
โ โProps are only default valuesโ
โ โChild updates props directlyโ
โ Correct understanding = strong React fundamentals
๐น One-Line Interview Answer
โProps are read-only data passed from parent to child components in React to make components reusable and dynamic.โ
