Skip to content
beginnerPhase 36 · React

Props

Pass data between components with props, default values, and prop types.

45m
0 problems
Topic Progress0%

Passing Props

Passing Props

Props are how you pass data from parent to child components.

Basic Props

// Parent
function App() {
  return <Greeting name="Alice" age={30} />;
}

// Child
function Greeting({ name, age }) {
  return (
    <div>
      <h1>Hello, {name}!</h1>
      <p>You are {age} years old.</p>
    </div>
  );
}

Props are Read-Only

function Counter({ count }) {
  // count++ = count + 1; // This doesn't work!
  // Props are immutable
  return <p>Count: {count}</p>;
}

Passing Different Types

function UserProfile({ user, isActive, onClick }) {
  return (
    <div className={isActive ? "active" : ""}>
      <h2>{user.name}</h2>
      <button onClick={onClick}>Click me</button>
    </div>
  );
}

// Usage
<UserProfile
  user={{ name: "Alice", email: "alice@example.com" }}
  isActive={true}
  onClick={() => console.log("clicked")}
/>

Spreading Props

function Button({ label, ...rest }) {
  return <button {...rest}>{label}</button>;
}

// Usage
const props = { label: "Click", className: "btn", disabled: false };
<Button {...props} />

Default Props

Default Props

Default Parameter Values (Recommended)

function Button({ label = "Click me", variant = "primary" }) {
  return (
    <button className={`btn btn-${variant}`}>
      {label}
    </button>
  );
}

// Usage
<Button /> // label="Click me", variant="primary"
<Button label="Submit" /> // variant="primary"
<Button variant="secondary" /> // label="Click me"

Default Props Object (Legacy)

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

Greeting.defaultProps = {
  name: "World"
};

// Usage
<Greeting /> // "Hello, World!"
<Greeting name="Alice" /> // "Hello, Alice!"

When to Use Each

  • Default parameters: Preferred, works with TypeScript
  • defaultProps: Legacy, avoid in new code

Complex Defaults

function UserCard({ user = { name: "Anonymous", avatar: "default.png" } }) {
  return (
    <div>
      <img src={user.avatar} alt={user.name} />
      <h2>{user.name}</h2>
    </div>
  );
}

Props Destructuring

Props Destructuring

Basic Destructuring

// Without destructuring
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// With destructuring
function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

Destructuring with Defaults

function Button({ label = "Click", variant = "primary", size = "md" }) {
  return (
    <button className={`btn btn-${variant} btn-${size}`}>
      {label}
    </button>
  );
}

Renaming Variables

function UserProfile({ name: userName, email: userEmail }) {
  return (
    <div>
      <h2>{userName}</h2>
      <p>{userEmail}</p>
    </div>
  );
}

Rest Parameters

function Button({ label, onClick, ...rest }) {
  return (
    <button onClick={onClick} {...rest}>
      {label}
    </button>
  );
}

// Usage
<Button
  label="Click"
  onClick={handleClick}
  className="btn"
  disabled={false}
/>

Destructuring in Different Positions

// In function parameters
function Comp({ a, b }) { ... }

// Inside function body
function Comp(props) {
  const { a, b } = props;
  // ...
}

// In arrow functions
const Comp = ({ a, b }) => <div>{a}{b}</div>;

Practice Problems

0/3solved
Build Props Component

Create a reusable React component implementing Props. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Props Testing

Write unit and integration tests for Props using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Props Performance

Optimize Props for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What are props?

Question 1 options

2. Are props mutable?

Question 2 options

3. How do you set default prop values?

Question 3 options

4. What does the spread operator do with props?

Question 4 options

Flashcards

Question

What are props?

Answer

Data passed from parent to child components

Question

Can you modify props in a child component?

Answer

No, props are read-only

Question

How do you destructure props?

Answer

function Comp({ prop1, prop2 }) { }

Question

What is prop spreading?

Answer

Using ...props to pass all properties as individual props

Question

What is Props?

Answer

Props is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Props are read-only data passed from parent to child
  • 2.Use default parameter values for defaults
  • 3.Destructure props for cleaner code
  • 4.Spread operator can pass all props
  • 5.Props flow one way: parent to child

Interview Tips

  • Explain that props are immutable
  • Show how to set default props
  • Demonstrate destructuring and spreading

Cheat Sheet

Cheat Sheet

Passing Props

<Child name="Alice" age={30} />
function Child({ name, age }) { ... }

Default Values

function Comp({ prop = "default" }) { ... }

Destructuring

function Comp({ name, age, ...rest }) { ... }

Spreading

<Child {...allProps} />
function Child({ label, ...rest }) {
  return <button {...rest}>{label}</button>;
}