Skip to main content

๐Ÿ“˜ React Props โ€” Complete In-Depth Theory Guide


1. Introduction

๐Ÿ”น What are Props?

Props (short for โ€œpropertiesโ€) are inputs passed from a parent component to a child component in React. They allow components to be:
  • Dynamic
  • Reusable
  • Configurable
๐Ÿ‘‰ Props are read-only and cannot be modified by the receiving component.

๐Ÿ”น Why Props are Important in React

Props are fundamental because they enable:
  • Component Reusability
    • Same component, different data
  • Separation of Concerns
    • Logic in parent, UI in child
  • Data Flow
    • Enables unidirectional data flow (top โ†’ down)

๐Ÿ”น When and Why We Use Props

Use props when:
  • You want to pass data to child components
  • You need to customize UI behavior
  • You want to share state across components
  • You are building composable UI systems

2. Concepts / Internal Workings

๐Ÿ”น Unidirectional Data Flow

React follows a one-way data flow:
Parent โ†’ Child โ†’ Grandchild
  • Props flow down the component tree
  • Child cannot directly modify parent data

๐Ÿ”น Props are Immutable

Props are read-only objects.
function Child(props) {
  props.name = "New Name"; // โŒ Not allowed
}
๐Ÿ‘‰ React enforces immutability to:
  • Prevent side effects
  • Ensure predictable UI updates

๐Ÿ”น How Props Work Internally

Under the hood:
  1. React creates a virtual DOM element
  2. Props are stored as an object inside it
  3. During rendering:
    • React compares old vs new props (diffing)
    • Updates only what changed
<MyComponent name="John" />
Internally becomes:
{
  type: MyComponent,
  props: { name: "John" }
}

๐Ÿ”น Props vs State

FeaturePropsState
OwnershipParentComponent itself
MutabilityImmutableMutable
PurposePass dataManage internal data

๐Ÿ”น Relationship with Other React Features

  • Hooks (useState, useEffect)
    • Props often trigger effects
  • Context API
    • Alternative to passing props deeply (prop drilling)
  • Memoization (React.memo)
    • Props are used for comparison to prevent re-renders

3. Syntax & Examples


๐Ÿ”น Basic Props Passing

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

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

๐Ÿ”น Destructuring Props

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

๐Ÿ”น Passing Multiple Props

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

<User name="John" age={25} />

๐Ÿ”น Default Props

function Button({ text = "Click Me" }) {
  return <button>{text}</button>;
}

๐Ÿ”น Passing Functions as Props

function Button({ onClick }) {
  return <button onClick={onClick}>Click</button>;
}

function App() {
  const handleClick = () => alert("Clicked!");

  return <Button onClick={handleClick} />;
}
๐Ÿ‘‰ Enables child โ†’ parent communication

๐Ÿ”น Passing Objects & Arrays

function Profile({ user }) {
  return <h2>{user.name}</h2>;
}

const user = { name: "Alice" };

<Profile user={user} />;

๐Ÿ”น Children Prop

function Card({ children }) {
  return <div className="card">{children}</div>;
}

<Card>
  <h2>Title</h2>
  <p>Description</p>
</Card>
๐Ÿ‘‰ children is a special prop

๐Ÿ”น Spread Props

const props = { name: "John", age: 30 };

<User {...props} />

๐Ÿ”น Conditional Props

<Button disabled={isLoading} />

๐Ÿ”น Inline Expressions

<User age={20 + 5} />

4. Edge Cases / Common Mistakes


โŒ Mutating Props

function Child(props) {
  props.value = 10; // โŒ
}
โœ… Fix: Use state or callbacks

โŒ Prop Drilling

<App>
  <Parent>
    <Child>
      <GrandChild data={data} />
    </Child>
  </Parent>
</App>
๐Ÿ‘‰ Problem: Passing props through many layers โœ… Solution:
  • Context API
  • State management (Redux, Zustand)

โŒ Re-render Issues with Objects/Functions

<MyComponent obj={{ name: "John" }} />
๐Ÿ‘‰ New object created every render โ†’ unnecessary re-renders โœ… Fix:
const obj = useMemo(() => ({ name: "John" }), []);

โŒ Missing Props

function User({ name }) {
  return <p>{name.toUpperCase()}</p>;
}
๐Ÿ‘‰ Crash if name is undefined โœ… Fix:
function User({ name = "" }) {
  return <p>{name.toUpperCase()}</p>;
}

โŒ Incorrect Type Usage

Passing wrong types can break UI. โœ… Solution:
  • Use TypeScript or PropTypes

โŒ Overusing Props

Too many props โ†’ hard to maintain
<Component a b c d e f g />
โœ… Fix:
  • Group into objects
  • Refactor components

5. Best Practices


โœ… Keep Props Minimal

  • Pass only what is needed
  • Avoid unnecessary data

โœ… Use Destructuring

function Card({ title, description }) {}
Improves readability

โœ… Use Meaningful Names

<User isLoggedIn /> // โœ…
<User flag />       // โŒ

โœ… Avoid Inline Functions (When Needed)

<Button onClick={() => handleClick()} /> // โŒ
Better:
<Button onClick={handleClick} /> // โœ…

โœ… Memoization for Performance

Use:
  • React.memo
  • useMemo
  • useCallback
To prevent unnecessary re-renders

โœ… Validate Props

Using TypeScript:
type Props = {
  name: string;
};

โœ… Component Composition over Props Explosion

Instead of:
<Modal title="Title" content="Content" footer="Footer" />
Prefer:
<Modal>
  <Header />
  <Body />
  <Footer />
</Modal>

โœ… Avoid Deep Prop Drilling

Use:
  • Context API
  • Global state management

โœ… Keep Components Pure

Props in โ†’ UI out
function PureComponent({ value }) {
  return <div>{value}</div>;
}

๐Ÿ”š Summary

Props are:
  • Immutable inputs
  • Used for component communication
  • Key to reusability and composition
Mastering props helps you:
  • Design scalable UI
  • Optimize performance
  • Write maintainable React code

๐Ÿ“˜ React Props โ€” Senior-Level Conceptual Interview Questions


1. Why are props immutable in React? What would break if they were mutable?

โœ… Strong Answer

Props are immutable to maintain predictability and consistency in rendering.

WHY:

  • React relies on pure component rendering
  • Immutability enables efficient diffing (reconciliation)
  • Prevents side effects across component boundaries
If props were mutable:
  • Child components could silently modify parent data
  • Reactโ€™s Virtual DOM diffing would become unreliable
  • Debugging becomes extremely difficult due to hidden mutations
function Child({ user }) {
  user.name = "Hacked"; // โŒ breaks data integrity
}

Comparison:

  • Props โ†’ Immutable (controlled externally)
  • State โ†’ Mutable (controlled internally)

2. How does React detect prop changes during reconciliation?

โœ… Strong Answer

React uses shallow comparison of props during reconciliation.

HOW:

  • When a component re-renders:
    • React compares previous props vs new props
    • If different โ†’ re-render child
<MyComponent obj={{ name: "John" }} />
This creates a new object reference each render, so React sees it as changed.

WHY:

  • Shallow comparison is fast (O(n))
  • Deep comparison would be expensive

Optimization:

const obj = useMemo(() => ({ name: "John" }), []);

Alternative:

  • React.memo() for functional components

3. Explain prop drilling. When is it actually acceptable?

โœ… Strong Answer

Prop drilling is passing props through multiple intermediate components.

Problem:

  • Intermediate components donโ€™t use the data
  • Leads to tight coupling and poor maintainability

When itโ€™s OK:

  • Small component trees
  • Clear ownership of data
  • Avoids unnecessary abstraction

When NOT:

  • Deep trees
  • Frequently changing data

Alternatives:

  • Context API
  • State management libraries
<Parent>
  <Child>
    <GrandChild data={data} />
  </Child>
</Parent>

4. What are the trade-offs between props and Context API?

โœ… Strong Answer

PropsContext
ExplicitImplicit
Easy to traceHarder to debug
Good for local dataGood for global/shared data

Trade-offs:

Props
  • โœ… Transparent data flow
  • โŒ Verbose (prop drilling)
Context
  • โœ… Eliminates drilling
  • โŒ Can cause unnecessary re-renders

Key Insight:

Context is not a replacement for props โ€” itโ€™s a complement

5. Why do inline objects/functions in props cause performance issues?

โœ… Strong Answer

Because they create new references on every render.
<MyComponent onClick={() => doSomething()} />
React sees:
  • Old function !== New function โ†’ triggers re-render

WHY:

  • JavaScript compares objects/functions by reference

Fix:

const handleClick = useCallback(() => doSomething(), []);
<MyComponent onClick={handleClick} />

When it matters:

  • In memoized components (React.memo)

6. How do props enable inversion of control in React?

โœ… Strong Answer

Props allow parent components to control child behavior.

Example:

function Button({ onClick }) {
  return <button onClick={onClick}>Click</button>;
}
Parent decides behavior:
<Button onClick={handleSubmit} />

WHY:

  • Promotes flexibility and reusability
  • Decouples logic from UI

Alternative:

  • Hardcoding logic inside component โ†’ less reusable

7. What is the โ€œchildrenโ€ prop and why is it powerful?

โœ… Strong Answer

children allows components to act as wrappers or layouts.
<Card>
  <h1>Title</h1>
</Card>

WHY powerful:

  • Enables composition over configuration
  • Avoids prop explosion

Alternative:

<Card title="Title" content="..." /> // less flexible

8. How does React.memo interact with props?

โœ… Strong Answer

React.memo prevents re-render if props havenโ€™t changed (shallow comparison).
const MyComponent = React.memo(({ value }) => {
  return <div>{value}</div>;
});

Pitfall:

<MyComponent obj={{ a: 1 }} /> // always re-renders

WHY:

  • New reference each render

Fix:

  • Memoize props
  • Use custom comparison function

9. What happens when props change? Describe the full lifecycle.

โœ… Strong Answer

  1. Parent re-renders
  2. New props passed to child
  3. React compares old vs new props
  4. If changed:
    • Child re-renders
    • Effects may run (useEffect)

Important:

  • Even if props are same โ†’ child may still re-render (unless memoized)

10. Can a component re-render without prop changes?

โœ… Strong Answer

Yes.

Reasons:

  • Parent re-render
  • Internal state changes
  • Context updates
function Parent() {
  const [count, setCount] = useState(0);
  return <Child name="John" />;
}
Child re-renders even if name didnโ€™t change.

Optimization:

  • React.memo

11. Why is passing large objects as props risky?

โœ… Strong Answer

  • Causes frequent re-renders
  • Harder to track changes
  • Breaks memoization

Better:

<User name={user.name} />
Instead of:
<User user={user} />

Trade-off:

  • Granularity vs convenience

12. How do you handle optional props safely?

โœ… Strong Answer

Use:
  • Default values
  • Type checking
function User({ name = "Guest" }) {
  return <p>{name}</p>;
}

WHY:

Prevents runtime errors:
name.toUpperCase() // crash if undefined

13. Explain controlled vs uncontrolled behavior using props.

โœ… Strong Answer

Controlled component:
  • Parent controls value via props
<input value={value} onChange={handleChange} />
Uncontrolled:
  • Component manages its own state

WHY:

  • Controlled โ†’ predictable
  • Uncontrolled โ†’ simpler but less control

14. What are the dangers of โ€œprop explosionโ€?

โœ… Strong Answer

Too many props:
<Component a b c d e f />

Problems:

  • Hard to maintain
  • Poor readability
  • Tight coupling

Solutions:

  • Group into objects
  • Use composition

15. When should you derive state from props (and when not)?

โœ… Strong Answer

Avoid:

const [value, setValue] = useState(props.value);

WHY:

  • Leads to stale state
  • Sync issues

Use only when:

  • Need to transform props once

Alternative:

  • Compute directly from props
  • Use memoization

16. How do props interact with hooks like useEffect?

โœ… Strong Answer

Props can trigger effects:
useEffect(() => {
  fetchData(id);
}, [id]);

WHY:

  • Dependency array watches prop changes

Pitfall:

  • Missing dependency โ†’ stale data
  • Over-dependency โ†’ unnecessary calls

17. What is a render prop pattern and how does it differ from normal props?

โœ… Strong Answer

A render prop is a function prop that returns JSX.
<DataFetcher render={(data) => <UI data={data} />} />

WHY:

  • Enables dynamic rendering logic

vs Normal Props:

  • Normal โ†’ static data
  • Render prop โ†’ dynamic UI logic

18. How would you design a highly reusable component using props?

โœ… Strong Answer

Principles:
  • Keep props minimal
  • Use children
  • Accept behavior via callbacks
function Modal({ isOpen, onClose, children }) {
  if (!isOpen) return null;
  return <div onClick={onClose}>{children}</div>;
}

WHY:

  • Flexible
  • Composable
  • Decoupled

๐Ÿ”š Final Takeaway

Senior-level understanding of props is about:
  • Data flow architecture
  • Performance implications
  • Component design philosophy
  • Trade-offs between abstraction vs simplicity

๐Ÿ“˜ React Props โ€” Senior-Level MCQs (Deep Understanding)


1. What happens in the following scenario?

const Child = ({ data }) => {
  console.log("Child render");
  return <div>{data.value}</div>;
};

function Parent() {
  const data = { value: 1 };
  return <Child data={data} />;
}

Options:

A. Child renders only once B. Child re-renders on every Parent render C. Child never re-renders D. React throws an error

โœ… Correct Answer: B

โœ” Explanation:

  • A new object is created on every render โ†’ new reference
  • React sees data as changed โ†’ triggers re-render

โŒ Why others are wrong:

  • A: Incorrect โ€” reference changes every time
  • C: Impossible unless memoized
  • D: No error here

2. What will prevent unnecessary re-renders here?

<Child config={{ theme: "dark" }} />

Options:

A. Wrap Child with React.memo only B. Use useMemo for config C. Use useCallback D. Nothing can prevent it

โœ… Correct Answer: B

โœ” Explanation:

  • Object literal creates new reference
  • useMemo stabilizes it
const config = useMemo(() => ({ theme: "dark" }), []);

โŒ Why others are wrong:

  • A: Wonโ€™t help alone (props still change)
  • C: For functions, not objects
  • D: Incorrect โ€” optimization exists

3. What happens if props are mutated inside a child?

function Child(props) {
  props.count = 10;
}

Options:

A. React prevents mutation automatically B. Parent state updates C. Silent mutation causing bugs D. Component crashes

โœ… Correct Answer: C

โœ” Explanation:

  • JS allows mutation โ†’ React doesnโ€™t block it
  • Leads to unexpected side effects

โŒ Why others are wrong:

  • A: No enforcement at runtime
  • B: No automatic sync
  • D: No crash unless used incorrectly

4. Why does this component re-render?

const Child = React.memo(({ onClick }) => {
  console.log("Render");
  return <button onClick={onClick}>Click</button>;
});

<Child onClick={() => console.log("Hi")} />

Options:

A. React.memo doesnโ€™t work with functions B. Function is recreated each render C. Event handlers always trigger re-render D. Memo only works with state

โœ… Correct Answer: B

โœ” Explanation:

  • New function reference each render โ†’ fails shallow comparison

โŒ Why others are wrong:

  • A: Memo works fine with functions
  • C: Events donโ€™t trigger re-render by default
  • D: Memo works with props

5. What is the best fix?

Options:

A. Move function outside component B. Use useCallback C. Use useEffect D. No fix needed

โœ… Correct Answer: B

โœ” Explanation:

const handleClick = useCallback(() => {
  console.log("Hi");
}, []);

โŒ Why others are wrong:

  • A: May break access to state
  • C: Irrelevant
  • D: Incorrect

6. What will happen?

function Child({ user }) {
  return <p>{user.name}</p>;
}

<Child />

Options:

A. Renders empty B. Throws runtime error C. React warns but works D. React auto-fills props

โœ… Correct Answer: B

โœ” Explanation:

  • user is undefined โ†’ user.name crashes

โŒ Why others are wrong:

  • A: No fallback
  • C: No automatic handling
  • D: React doesnโ€™t infer props

7. What is the best fix?

Options:

A. Optional chaining B. Default props C. Both A and B D. Wrap in try-catch

โœ… Correct Answer: C

โœ” Explanation:

function Child({ user = {} }) {
  return <p>{user?.name}</p>;
}

โŒ Why others are wrong:

  • A/B alone may not fully cover
  • D: Overkill

8. What is the issue here?

function Parent() {
  const [count, setCount] = useState(0);
  return <Child value={count} />;
}

Options:

A. Child wonโ€™t update B. Child always re-renders C. Props cause memory leak D. Infinite loop

โœ… Correct Answer: B

โœ” Explanation:

  • Parent state change โ†’ re-render โ†’ child re-renders

โŒ Why others are wrong:

  • A: Incorrect
  • C/D: Not applicable

9. How to optimize the child?

Options:

A. React.memo B. useEffect C. useReducer D. useRef

โœ… Correct Answer: A


10. What happens with prop drilling?

Options:

A. Improves performance B. Increases coupling C. Reduces bugs D. Avoids re-renders

โœ… Correct Answer: B

โœ” Explanation:

  • Intermediate components depend on props unnecessarily

11. Which is better for deeply nested data?

Options:

A. Props B. Context API C. Inline functions D. useEffect

โœ… Correct Answer: B


12. What happens here?

const Child = React.memo(({ data }) => {
  console.log("Render");
  return null;
});

const data = { a: 1 };

<Child data={data} />

Options:

A. Always re-renders B. Never re-renders C. Re-renders only if reference changes D. Crashes

โœ… Correct Answer: C


13. What is wrong with this pattern?

const [value, setValue] = useState(props.value);

Options:

A. Nothing B. Causes stale state issues C. Causes infinite loop D. Slows rendering

โœ… Correct Answer: B

โœ” Explanation:

  • State wonโ€™t update if prop changes

14. What is the better approach?

Options:

A. useEffect sync B. Derive directly from props C. Global state D. Ignore updates

โœ… Correct Answer: B


15. Why is children preferred over multiple props?

Options:

A. Faster rendering B. Avoids prop explosion C. Required by React D. Works only in class components

โœ… Correct Answer: B


16. What happens here?

<Child key="1" value={10} />
Then:
<Child key="2" value={10} />

Options:

A. Props update only B. Component remounts C. No change D. Error

โœ… Correct Answer: B

โœ” Explanation:

  • Key change โ†’ React treats as new component

17. Which causes unnecessary re-renders?

Options:

A. Stable primitive props B. Inline objects/functions C. useMemo values D. Static props

โœ… Correct Answer: B


18. What is the main purpose of props?

Options:

A. Manage internal state B. Enable data flow and composition C. Replace hooks D. Optimize rendering

โœ… Correct Answer: B


๐Ÿ”š Final Insight

These questions test:
  • Reference equality vs value equality
  • Rendering behavior
  • Component design decisions
  • Performance optimization

๐Ÿ“˜ React Props โ€” Real-World Coding Problems (Senior Level)


๐Ÿงฉ Problem 1: Dynamic Profile Card System

๐Ÿ“Œ Problem Statement

Build a reusable ProfileCard component that receives user data via props and renders different layouts based on user type.

๐Ÿ”’ Constraints

  • Props: { user, variant }
  • variant can be "compact" or "detailed"
  • Must not mutate props

โœ… Expected Behavior

  • Compact โ†’ name + avatar
  • Detailed โ†’ name + avatar + bio + stats

โš ๏ธ Edge Cases

  • Missing user fields
  • Unknown variant

๐Ÿ’ก Solution

function ProfileCard({ user = {}, variant = "compact" }) {
  if (variant === "detailed") {
    return (
      <div>
        <img src={user.avatar} />
        <h2>{user.name}</h2>
        <p>{user.bio}</p>
      </div>
    );
  }
  return <h2>{user.name}</h2>;
}

๐Ÿง  Explanation

  • Uses default props to avoid crashes
  • Conditional rendering based on props

๐Ÿงฉ Problem 2: Controlled Form Input Wrapper

๐Ÿ“Œ Problem Statement

Create an InputField component that behaves as a controlled component via props.

๐Ÿ”’ Constraints

  • Props: { value, onChange }
  • Must not manage internal state

โœ… Expected Behavior

  • Value updates only via parent

โš ๏ธ Edge Cases

  • Undefined value
  • onChange not passed

๐Ÿ’ก Solution

function InputField({ value = "", onChange }) {
  return <input value={value} onChange={onChange} />;
}

๐Ÿง  Explanation

  • Demonstrates controlled component via props

๐Ÿงฉ Problem 3: Prevent Unnecessary Re-renders

๐Ÿ“Œ Problem Statement

Optimize a ListItem component receiving object props.

๐Ÿ”’ Constraints

  • Must avoid unnecessary re-renders

โš ๏ธ Edge Cases

  • Parent re-renders frequently

๐Ÿ’ก Solution

const ListItem = React.memo(({ item }) => {
  return <li>{item.name}</li>;
});
Parent:
const item = useMemo(() => ({ name: "Item" }), []);
<ListItem item={item} />;

๐Ÿง  Explanation

  • Combines React.memo + stable props

๐Ÿงฉ Problem 4: Modal with Render Props

๐Ÿ“Œ Problem Statement

Build a modal that receives UI via a render prop.

๐Ÿ”’ Constraints

  • Props: { render }

๐Ÿ’ก Solution

function Modal({ render }) {
  return <div className="modal">{render()}</div>;
}
Usage:
<Modal render={() => <p>Hello</p>} />

๐Ÿง  Explanation

  • Demonstrates render prop pattern

๐Ÿงฉ Problem 5: Button with Behavior Injection

๐Ÿ“Œ Problem Statement

Create a reusable button that receives behavior via props.

๐Ÿ’ก Solution

function Button({ onClick, children }) {
  return <button onClick={onClick}>{children}</button>;
}

๐Ÿง  Explanation

  • Shows inversion of control

๐Ÿงฉ Problem 6: Deep Prop Drilling Refactor

๐Ÿ“Œ Problem Statement

Refactor a deeply nested component passing props through 4 levels.

๐Ÿ”’ Constraints

  • Avoid prop drilling

๐Ÿ’ก Solution

Use Context:
const DataContext = React.createContext();

<DataContext.Provider value={data}>
  <DeepChild />
</DataContext.Provider>

๐Ÿง  Explanation

  • Avoids unnecessary prop passing

๐Ÿงฉ Problem 7: Derived State Anti-pattern Fix

๐Ÿ“Œ Problem Statement

Fix component syncing state from props incorrectly.
const [value, setValue] = useState(props.value);

๐Ÿ’ก Solution

function Component({ value }) {
  return <div>{value}</div>;
}

๐Ÿง  Explanation

  • Avoids stale state issues

๐Ÿงฉ Problem 8: Safe Optional Props Rendering

๐Ÿ“Œ Problem Statement

Render optional nested data safely.

๐Ÿ’ก Solution

function User({ user }) {
  return <p>{user?.profile?.name || "Guest"}</p>;
}

๐Ÿงฉ Problem 9: Generic Table Component

๐Ÿ“Œ Problem Statement

Build a table that receives columns and data via props.

๐Ÿ”’ Constraints

  • Dynamic columns

๐Ÿ’ก Solution

function Table({ data, columns }) {
  return (
    <table>
      <thead>
        <tr>
          {columns.map(col => <th key={col}>{col}</th>)}
        </tr>
      </thead>
      <tbody>
        {data.map((row, i) => (
          <tr key={i}>
            {columns.map(col => <td key={col}>{row[col]}</td>)}
          </tr>
        ))}
      </tbody>
    </table>
  );
}

๐Ÿงฉ Problem 10: Prevent Function Prop Re-Creation

๐Ÿ“Œ Problem Statement

Optimize function props in a list.

๐Ÿ’ก Solution

const handleClick = useCallback((id) => {
  console.log(id);
}, []);

๐Ÿงฉ Problem 11: Compound Component Pattern

๐Ÿ“Œ Problem Statement

Create a Tabs system using props + children.

๐Ÿ’ก Solution

function Tabs({ children }) {
  return <div>{children}</div>;
}

๐Ÿงฉ Problem 12: Prop Validation with TypeScript

๐Ÿ“Œ Problem Statement

Ensure correct prop types.

๐Ÿ’ก Solution

type Props = {
  name: string;
};

๐Ÿงฉ Problem 13: Conditional Rendering Based on Props

๐Ÿ“Œ Problem Statement

Render loading or content.

๐Ÿ’ก Solution

function Loader({ isLoading, children }) {
  return isLoading ? <p>Loading...</p> : children;
}

๐Ÿงฉ Problem 14: Reusable Layout Wrapper

๐Ÿ“Œ Problem Statement

Create layout using children.

๐Ÿ’ก Solution

function Layout({ header, children, footer }) {
  return (
    <>
      {header}
      {children}
      {footer}
    </>
  );
}

๐Ÿงฉ Problem 15: Key Prop Behavior

๐Ÿ“Œ Problem Statement

Explain remount behavior when key changes.

๐Ÿ’ก Explanation

  • Changing key forces remount โ†’ resets state

๐Ÿงฉ Problem 16: Memoization Pitfall

๐Ÿ“Œ Problem Statement

Fix unnecessary re-renders with nested props.

๐Ÿ’ก Solution

  • Normalize props
  • Avoid passing full objects

๐Ÿงฉ Problem 17: Dynamic Theme Injection

๐Ÿ“Œ Problem Statement

Pass theme via props.

๐Ÿ’ก Solution

function Box({ theme }) {
  return <div style={{ color: theme.color }} />;
}

๐Ÿงฉ Problem 18: Higher-Order Component with Props

๐Ÿ“Œ Problem Statement

Wrap component and pass extra props.

๐Ÿ’ก Solution

function withUser(Component) {
  return function Wrapped(props) {
    return <Component {...props} user={{ name: "John" }} />;
  };
}

๐Ÿงฉ Problem 19: Prop Normalization Layer

๐Ÿ“Œ Problem Statement

Normalize inconsistent API response props.

๐Ÿ’ก Solution

const normalizedUser = {
  name: user.name || user.username
};

๐Ÿงฉ Problem 20: Performance Audit Scenario

๐Ÿ“Œ Problem Statement

Optimize a large list receiving props.

๐Ÿ’ก Solution

  • Use React.memo
  • Virtualization (React Window)
  • Stable props

๐Ÿ”š Final Takeaway

These problems test:
  • Real-world prop usage
  • Performance optimization
  • Architecture decisions
  • Debugging mindset

๐Ÿ› ๏ธ React Props โ€” Senior-Level Debugging Challenges (Production Scenarios)


๐Ÿงฉ 1. Infinite Re-render due to Derived State

โŒ Buggy Code

function Counter({ value }) {
  const [count, setCount] = useState(value);

  useEffect(() => {
    setCount(value);
  }, [count]); // โŒ wrong dependency

  return <p>{count}</p>;
}

๐Ÿ” Whatโ€™s Wrong

  • Effect depends on count but sets count โ†’ infinite loop

โ“ WHY it happens

  • Changing count triggers effect again โ†’ loop

โœ… Fix

useEffect(() => {
  setCount(value);
}, [value]);

๐Ÿง  Best Practice

  • Avoid syncing props to state unless necessary

๐Ÿงฉ 2. React.memo Not Working

โŒ Buggy Code

const Child = React.memo(({ config }) => {
  console.log("Rendered");
  return <div>{config.theme}</div>;
});

function Parent() {
  return <Child config={{ theme: "dark" }} />;
}

๐Ÿ” Whatโ€™s Wrong

  • Inline object causes new reference each render

โ“ WHY

  • Shallow comparison fails

โœ… Fix

const config = useMemo(() => ({ theme: "dark" }), []);
<Child config={config} />

๐Ÿง  Best Practice

  • Stabilize object/array props

๐Ÿงฉ 3. Unexpected Undefined Crash

โŒ Buggy Code

function Profile({ user }) {
  return <p>{user.name.toUpperCase()}</p>;
}

๐Ÿ” Whatโ€™s Wrong

  • user may be undefined

โ“ WHY

  • Props not guaranteed unless validated

โœ… Fix

function Profile({ user = {} }) {
  return <p>{user.name?.toUpperCase() || "Guest"}</p>;
}

๐Ÿง  Best Practice

  • Always guard optional props

๐Ÿงฉ 4. Function Prop Causing Re-renders

โŒ Buggy Code

<Child onClick={() => doSomething()} />

๐Ÿ” Whatโ€™s Wrong

  • New function every render

โ“ WHY

  • Breaks memoization

โœ… Fix

const handleClick = useCallback(doSomething, []);
<Child onClick={handleClick} />

๐Ÿง  Best Practice

  • Use useCallback for stable handlers

๐Ÿงฉ 5. Prop Mutation Bug

โŒ Buggy Code

function Child({ items }) {
  items.push("new"); // โŒ mutation
  return <div>{items.length}</div>;
}

๐Ÿ” Whatโ€™s Wrong

  • Mutating props

โ“ WHY

  • Breaks Reactโ€™s data flow

โœ… Fix

const newItems = [...items, "new"];

๐Ÿง  Best Practice

  • Treat props as immutable

๐Ÿงฉ 6. Stale Closure with Props

โŒ Buggy Code

function Timer({ delay }) {
  useEffect(() => {
    const id = setInterval(() => {
      console.log(delay);
    }, 1000);
    return () => clearInterval(id);
  }, []); // โŒ missing dependency
}

๐Ÿ” Whatโ€™s Wrong

  • delay is stale

โ“ WHY

  • Closure captures initial value

โœ… Fix

useEffect(() => {
  const id = setInterval(() => {
    console.log(delay);
  }, delay);
  return () => clearInterval(id);
}, [delay]);

๐Ÿง  Best Practice

  • Always include props in dependencies

๐Ÿงฉ 7. Over-rendering Child Components

โŒ Buggy Code

function Parent({ user }) {
  return <Child user={user} />;
}
Parent re-renders frequently.

๐Ÿ” Issue

  • Child re-renders unnecessarily

โ“ WHY

  • No memoization

โœ… Fix

const Child = React.memo(({ user }) => { ... });

๐Ÿง  Best Practice

  • Memoize pure components

๐Ÿงฉ 8. Incorrect Key Causing State Reset

โŒ Buggy Code

{items.map((item, index) => (
  <Item key={index} value={item} />
))}

๐Ÿ” Whatโ€™s Wrong

  • Index as key

โ“ WHY

  • Order changes โ†’ wrong reconciliation

โœ… Fix

key={item.id}

๐Ÿง  Best Practice

  • Use stable unique keys

๐Ÿงฉ 9. Conditional Props Breaking UI

โŒ Buggy Code

<Button disabled={isLoading && "true"} />

๐Ÿ” Whatโ€™s Wrong

  • Passing string instead of boolean

โ“ WHY

  • "true" is truthy but incorrect type

โœ… Fix

disabled={isLoading}

๐Ÿงฉ 10. Spreading Unnecessary Props

โŒ Buggy Code

<Component {...props} />

๐Ÿ” Issue

  • Passing unused props

โ“ WHY

  • Increases coupling, risk of bugs

โœ… Fix

<Component name={props.name} />

๐Ÿงฉ 11. Breaking Controlled Component

โŒ Buggy Code

<input value={value} />

๐Ÿ” Issue

  • Missing onChange

โ“ WHY

  • Becomes read-only

โœ… Fix

<input value={value} onChange={handleChange} />

๐Ÿงฉ 12. Recomputing Expensive Props

โŒ Buggy Code

<Chart data={processData(rawData)} />

๐Ÿ” Issue

  • Expensive computation every render

โ“ WHY

  • Function runs each render

โœ… Fix

const data = useMemo(() => processData(rawData), [rawData]);

๐Ÿงฉ 13. Nested Object Prop Issue

โŒ Buggy Code

<User settings={{ theme: { color: "blue" } }} />

๐Ÿ” Issue

  • Deep object recreated

โ“ WHY

  • Breaks memo

โœ… Fix

  • Lift and memoize object

๐Ÿงฉ 14. Children Misuse

โŒ Buggy Code

function Wrapper({ children }) {
  children = <div>{children}</div>; // โŒ mutation
}

๐Ÿ” Issue

  • Reassigning props

โœ… Fix

return <div>{children}</div>;

๐Ÿงฉ 15. Props Not Updating in Child

โŒ Buggy Code

const [value, setValue] = useState(props.value);

๐Ÿ” Issue

  • Not synced with prop changes

โœ… Fix

  • Use props directly OR sync via effect

๐Ÿงฉ 16. Boolean Prop Misinterpretation

โŒ Buggy Code

<MyComponent isActive="false" />

๐Ÿ” Issue

  • String is truthy

โœ… Fix

isActive={false}

๐Ÿงฉ 17. Overusing Props Instead of Composition

โŒ Buggy Code

<Card title="..." content="..." footer="..." />

๐Ÿ” Issue

  • Rigid API

โœ… Fix

<Card>
  <Header />
  <Body />
</Card>

๐Ÿงฉ 18. Missing Dependency Causing Buggy UI

โŒ Buggy Code

useEffect(() => {
  fetchData(id);
}, []); // โŒ

๐Ÿ” Issue

  • Doesnโ€™t react to prop change

โœ… Fix

[id]

๐Ÿ”š Final Takeaway

These bugs reflect real production issues:
  • Reference equality pitfalls
  • Stale closures
  • Improper memoization
  • Incorrect assumptions about props

๐Ÿ—๏ธ React Props โ€” Real-World Machine Coding Problems (Senior / Architect Level)

These are production-grade problems designed to test:
  • Component architecture
  • Data flow via props
  • Performance & scalability
  • Real-world trade-offs

๐Ÿงฉ 1. Configurable Data Table (Enterprise Grid)

๐Ÿ“Œ Requirements

  • Render dynamic table using props:
    • columns, data, renderCell, sortConfig
  • Support:
    • Sorting
    • Custom cell rendering
    • Column visibility toggle

๐Ÿ–ฅ๏ธ UI Behavior

  • Click column header โ†’ sort
  • Toggle columns โ†’ hide/show instantly

๐Ÿ”„ Data Flow

  • Parent owns data & sorting state
  • Table receives everything via props

โš ๏ธ Edge Cases

  • Empty data
  • Missing keys
  • Large datasets

โšก Performance

  • Virtualization (react-window)
  • Memoized rows

๐Ÿ—๏ธ Architecture

  • Table
  • TableRow
  • TableCell

๐Ÿง  Approach

  1. Normalize columns
  2. Pass render functions as props
  3. Memoize rows

๐Ÿงฉ 2. Headless Modal System

๐Ÿ“Œ Requirements

  • Build a modal with:
    • isOpen, onClose, children
  • No styling (headless)

๐Ÿ–ฅ๏ธ UI Behavior

  • Close on overlay click / ESC

๐Ÿ”„ Data Flow

  • Parent controls open/close

โš ๏ธ Edge Cases

  • Multiple modals
  • Focus trap

โšก Performance

  • Lazy mount/unmount

๐Ÿ—๏ธ Architecture

  • Modal
  • Portal

๐Ÿง  Approach

  • Use children for flexibility
  • Use props for behavior injection

๐Ÿงฉ 3. Multi-Step Form Wizard

๐Ÿ“Œ Requirements

  • Steps passed as props:
    • steps, currentStep, onNext, onBack

๐Ÿ–ฅ๏ธ UI Behavior

  • Navigate steps
  • Validate before next

๐Ÿ”„ Data Flow

  • Central form state in parent

โš ๏ธ Edge Cases

  • Skip steps
  • Validation errors

โšก Performance

  • Avoid re-rendering all steps

๐Ÿ—๏ธ Architecture

  • Wizard
  • Step

๐Ÿง  Approach

  • Render active step only
  • Pass handlers via props

๐Ÿงฉ 4. Reusable Dropdown with Custom Rendering

๐Ÿ“Œ Requirements

  • Props:
    • options, renderOption, onSelect

๐Ÿ–ฅ๏ธ UI Behavior

  • Keyboard navigation
  • Custom UI per option

โš ๏ธ Edge Cases

  • Empty list
  • Duplicate values

โšก Performance

  • Debounced filtering

๐Ÿง  Approach

  • Render prop for flexibility

๐Ÿงฉ 5. Infinite Scroll Feed

๐Ÿ“Œ Requirements

  • Props:
    • fetchData, renderItem

๐Ÿ–ฅ๏ธ UI Behavior

  • Load more on scroll

๐Ÿ”„ Data Flow

  • Parent handles API

โš ๏ธ Edge Cases

  • Duplicate fetch
  • End of list

โšก Performance

  • Intersection Observer

๐Ÿง  Approach

  • Controlled via props

๐Ÿงฉ 6. Theme System (Dynamic Styling)

๐Ÿ“Œ Requirements

  • Pass theme via props
  • Support light/dark

โš ๏ธ Edge Cases

  • Missing theme keys

โšก Performance

  • Memoize theme object

๐Ÿง  Approach

  • Theme provider or prop drilling trade-off

๐Ÿงฉ 7. Form Builder (Schema-driven UI)

๐Ÿ“Œ Requirements

  • Props:
    • schema, onSubmit

๐Ÿ–ฅ๏ธ UI Behavior

  • Dynamically render inputs

โš ๏ธ Edge Cases

  • Unknown field types

โšก Performance

  • Lazy render sections

๐Ÿง  Approach

  • Map schema โ†’ components

๐Ÿงฉ 8. Drag & Drop List

๐Ÿ“Œ Requirements

  • Props:
    • items, onReorder

โš ๏ธ Edge Cases

  • Rapid drag events

โšก Performance

  • Avoid full list re-render

๐Ÿง  Approach

  • Stable keys
  • Memoized items

๐Ÿงฉ 9. Notification System

๐Ÿ“Œ Requirements

  • Props:
    • notifications, onDismiss

๐Ÿ–ฅ๏ธ UI Behavior

  • Auto-dismiss

โš ๏ธ Edge Cases

  • Duplicate notifications

๐Ÿง  Approach

  • Controlled list via props

๐Ÿงฉ 10. Search with Debounced Input

๐Ÿ“Œ Requirements

  • Props:
    • onSearch

โšก Performance

  • Debounce input

๐Ÿงฉ 11. Reusable Card System (Composable UI)

๐Ÿ“Œ Requirements

  • Use children for layout slots

๐Ÿง  Approach

  • Composition over props explosion

๐Ÿงฉ 12. Chart Wrapper (Reusable Visualization)

๐Ÿ“Œ Requirements

  • Props:
    • data, config

โš ๏ธ Edge Cases

  • Empty dataset

โšก Performance

  • Memoized transformations

๐Ÿงฉ 13. Permission-based Rendering

๐Ÿ“Œ Requirements

  • Props:
    • permissions, children

๐Ÿง  Approach

if (!permissions.includes("admin")) return null;

๐Ÿงฉ 14. Lazy Image Component

๐Ÿ“Œ Requirements

  • Props:
    • src, placeholder

โšก Performance

  • Intersection Observer

๐Ÿงฉ 15. File Upload Component

๐Ÿ“Œ Requirements

  • Props:
    • onUpload, accept

โš ๏ธ Edge Cases

  • Invalid file types

๐Ÿงฉ 16. Pagination Component

๐Ÿ“Œ Requirements

  • Props:
    • page, totalPages, onChange

๐Ÿงฉ 17. Headless Tooltip System

๐Ÿ“Œ Requirements

  • Props:
    • content, children

๐Ÿงฉ 18. Virtualized List

๐Ÿ“Œ Requirements

  • Props:
    • items, rowHeight

โšก Performance

  • Render visible rows only

๐Ÿงฉ 19. Undo/Redo System

๐Ÿ“Œ Requirements

  • Props:
    • state, onUndo, onRedo

๐Ÿงฉ 20. Live Collaborative Cursor Tracker

๐Ÿ“Œ Requirements

  • Props:
    • users, positions

โš ๏ธ Edge Cases

  • Rapid updates

โšก Performance

  • Throttle updates

๐Ÿ”š Final Architect Insight

These problems test:
  • Prop-driven architecture
  • Component composition vs configuration
  • Performance tuning
  • Scalable design patterns

๐Ÿ“˜ React Props โ€” FAANG-Level Interview Questions (Senior Depth)


1. How would you design a component API using props for long-term scalability?

๐Ÿ” Follow-up

  • How do you prevent prop explosion?
  • When would you switch to composition?

โœ… Strong Answer

  • Start with minimal, explicit props
  • Prefer composition (children) over configuration
  • Group related props into objects when needed
  • Design for extensibility without breaking changes
<Modal>
  <Header />
  <Body />
</Modal>

WHY:

  • Reduces coupling
  • Improves flexibility

โŒ Weak Answer

  • โ€œJust add more props as neededโ€ ๐Ÿ‘‰ Fails because it ignores scalability and API design

2. Explain how React decides whether to re-render a component based on props.

๐Ÿ” Follow-up

  • How does React.memo change this?
  • What are its limitations?

โœ… Strong Answer

  • By default, React re-renders when parent renders
  • React.memo does shallow comparison of props

Key Insight:

  • Reference equality matters

โŒ Weak Answer

  • โ€œReact only re-renders when props changeโ€ ๐Ÿ‘‰ Incorrect โ€” parent re-render triggers child too

3. You notice a component re-rendering frequently despite unchanged data. How do you debug it?

๐Ÿ” Follow-up

  • What tools would you use?

โœ… Strong Answer

  • Check:
    • Inline objects/functions
    • Parent re-renders
    • Missing memoization
  • Use:
    • React DevTools Profiler

โŒ Weak Answer

  • โ€œAdd React.memo everywhereโ€ ๐Ÿ‘‰ Over-optimization without understanding cause

4. When would you avoid passing an object as a prop?

๐Ÿ” Follow-up

  • When is it acceptable?

โœ… Strong Answer

  • Avoid when:
    • Object recreated each render
    • Component is memoized
  • Acceptable when:
    • Stable reference (useMemo)
    • Small apps

โŒ Weak Answer

  • โ€œObjects are always badโ€ ๐Ÿ‘‰ Oversimplification

5. Design a reusable table component. What props would you expose?

๐Ÿ” Follow-up

  • How do you support custom rendering?

โœ… Strong Answer

  • Props:
    • data, columns, renderCell
  • Use render props for flexibility

โŒ Weak Answer

  • โ€œJust pass data and map itโ€ ๐Ÿ‘‰ Not scalable or flexible

6. How do you handle deeply nested data without prop drilling?

๐Ÿ” Follow-up

  • Trade-offs of Context API?

โœ… Strong Answer

  • Use Context for global/shared data
  • Avoid overuse (causes re-renders)

โŒ Weak Answer

  • โ€œAlways use Contextโ€ ๐Ÿ‘‰ Ignores trade-offs

7. What are the risks of syncing props to state?

๐Ÿ” Follow-up

  • When is it justified?

โœ… Strong Answer

  • Risks:
    • Stale state
    • Sync bugs
  • Use only for:
    • Derived/controlled transformations

โŒ Weak Answer

  • โ€œItโ€™s fine to copy props into stateโ€ ๐Ÿ‘‰ Leads to bugs

8. How would you design a component that allows behavior injection?

๐Ÿ” Follow-up

  • Compare with HOCs

โœ… Strong Answer

  • Use function props (callbacks)
<Button onClick={handleClick} />

โŒ Weak Answer

  • โ€œHardcode logicโ€ ๐Ÿ‘‰ Not reusable

9. Why does this cause unnecessary re-renders?

<Child config={{ theme: "dark" }} />

๐Ÿ” Follow-up

  • How do you fix it?

โœ… Strong Answer

  • New object reference each render
Fix:
useMemo(() => ({ theme: "dark" }), [])

โŒ Weak Answer

  • โ€œReact is inefficientโ€ ๐Ÿ‘‰ Misunderstanding core behavior

10. How do you decide between props vs global state?

๐Ÿ” Follow-up

  • What are scaling concerns?

โœ… Strong Answer

  • Props:
    • Local, predictable
  • Global state:
    • Shared, complex

โŒ Weak Answer

  • โ€œUse Redux for everythingโ€ ๐Ÿ‘‰ Overengineering

11. What is the role of children in API design?

๐Ÿ” Follow-up

  • When not to use it?

โœ… Strong Answer

  • Enables composition
  • Avoids rigid APIs

โŒ Weak Answer

  • โ€œItโ€™s just for nestingโ€ ๐Ÿ‘‰ Misses design importance

12. A component is slow due to heavy prop computation. How do you optimize?

๐Ÿ” Follow-up

  • Trade-offs of memoization?

โœ… Strong Answer

  • Use useMemo
  • Move computation outside render

โŒ Weak Answer

  • โ€œUse useEffectโ€ ๐Ÿ‘‰ Wrong tool

13. Explain how prop changes trigger effects.

๐Ÿ” Follow-up

  • What if dependency is missing?

โœ… Strong Answer

  • useEffect runs when dependencies change

โŒ Weak Answer

  • โ€œEffects run on every renderโ€ ๐Ÿ‘‰ Incorrect

14. How would you design a headless component using props?

๐Ÿ” Follow-up

  • Why is this pattern useful?

โœ… Strong Answer

  • Logic via props, UI via children

โŒ Weak Answer

  • โ€œJust build UI directlyโ€ ๐Ÿ‘‰ Not reusable

15. How do you prevent unnecessary prop spreading?

๐Ÿ” Follow-up

  • When is spreading useful?

โœ… Strong Answer

  • Explicit props preferred
  • Spread only for wrappers

โŒ Weak Answer

  • โ€œAlways use spreadโ€ ๐Ÿ‘‰ Risky

16. What happens when a key changes but props donโ€™t?

๐Ÿ” Follow-up

  • Real-world use case?

โœ… Strong Answer

  • Component remounts โ†’ state reset

โŒ Weak Answer

  • โ€œNothing changesโ€ ๐Ÿ‘‰ Incorrect

17. How do you handle optional props in large systems?

๐Ÿ” Follow-up

  • Type safety strategies?

โœ… Strong Answer

  • Default values
  • TypeScript

โŒ Weak Answer

  • โ€œJust check manuallyโ€ ๐Ÿ‘‰ Not scalable

18. Describe a real-world bug caused by props and how youโ€™d fix it.

๐Ÿ” Follow-up

  • How to prevent it?

โœ… Strong Answer

  • Example:
    • Inline object โ†’ re-render loop
  • Fix:
    • Memoization

โŒ Weak Answer

  • โ€œIโ€™d debug in consoleโ€ ๐Ÿ‘‰ Lacks depth

๐Ÿ” Follow-up

  • Metrics youโ€™d track?

โœ… Strong Answer

  • Use Profiler
  • Check:
    • Re-render frequency
    • Prop stability

โŒ Weak Answer

  • โ€œRewrite everythingโ€ ๐Ÿ‘‰ Unrealistic

๐Ÿ”š Final Interview Insight

At FAANG level, props questions test:
  • Architectural thinking
  • Trade-offs (simplicity vs scalability)
  • Performance awareness
  • Debugging intuition