Created
Feb 8, 2026
Last Modified
2 months ago

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:

javascript
Parent Component โžœ Child Component

Example

javascript
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.

javascript
<User name="Naveen" />
<User name="Amit" />
<User name="Riya" />
javascript
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

javascript
<Button text="Login" />
<Button text="Register" />

๐Ÿ”น Props are Read-Only (Very Important)

โŒ You cannot modify props inside the child component.

javascript
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.

javascript
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

javascript
function User({ name = "Guest" }) {
  return <p>Hello {name}</p>;
}

๐Ÿ”น Props with Destructuring (Best Practice)

javascript
function User({ name, age }) {
  return (
    <p>
      {name} - {age}
    </p>
  );
}

โœ” Cleaner
โœ” Readable
โœ” Professional


๐Ÿ”น Passing Multiple Data Types

javascript
<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

javascript
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.โ€