1. Home
  2. Coding Hub
  3. Coding Notes
  4. React
  5. Destructuring Props
45% OFFBLACK FRIDAY SALE
Sale ends in
03Days
00Hrs
19Mins
40Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. React
  5. Destructuring Props

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

03Days
00Hrs
19Mins
40Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
React IntroductionSetup & ViteJSX SyntaxVirtual DOMFunction ComponentsComponent TreeReact Fragments
Passing PropsDestructuring PropsChildren PropConditional RenderingList RenderingReact KeysCSS Modules
Event HandlingSynthetic EventsState BasicsuseState HookState BatchingObject StateArray State
useEffect HookDependency ArrayCleanup FunctionsData FetchingStrict Mode
useRef HookPassing Refs (forwardRef)useReducer HookCustom HooksuseId Hook
Prop Drilling ProblemContext APIuseContext HookRedux IntroZustand Alternative
React Router DOMRoute ParametersNested RoutesControlled InputsReact Hook Form
React.memouseMemo HookuseCallback HookCode Splitting (lazy)Bundle Size Optimization
React Server ComponentsClient ComponentsSuspense for Data FetchingNext.js App RouterServer ActionsuseTransition HookReact 19 Forms (useActionState)
React.js

Destructuring Props

Overview

When a component receives data, React automatically bundles all the passed attributes into a single JavaScript object called props. If you pass 5 attributes, you constantly have to write props.title, props.price, props.image, etc., throughout your component. This makes your code unnecessarily wordy and harder to read.

To solve this, React developers universally use JavaScript Object Destructuring directly in the function signature. Destructuring allows you to unpack specific properties from the props object and assign them to local variables immediately as the function receives them.

Not only does this make your JSX much cleaner by removing the repetitive props. prefix, but it also clearly documents exactly which data a component expects right at the very top of the file, making it dramatically easier for other engineers to understand your code.

Syntax

Standard vs Destructured Syntax
jsx
// BAD (Old fashioned): Repetitive props. prefix
function ProductCard(props) {
  return (
    <div>
      <h2>{props.name}</h2>
      <p>{props.price}</p>
      <img src={props.image} alt={props.name} />
    </div>
  );
}

// GOOD (Modern Industry Standard): Destructuring in the signature
function ProductCard({ name, price, image, isSale = false }) {
  return (
    <div>
      {isSale && <span>SALE!</span>}
      <h2>{name}</h2>
      <p>{price}</p>
      <img src={image} alt={name} />
    </div>
  );
}

Common Pitfalls

  • Forgetting the curly braces: If you write function Card(title, desc), React will assign the entire props object to the variable title, and desc will be undefined. You must wrap them in braces to destructure: function Card({ title, desc }).

Interview Questions

How do you provide a default value for a prop that might not be passed by the parent?

You can assign default values directly during destructuring using the equals sign: function User({ avatar = 'default.png', name }). If the parent does not pass an avatar prop, it will safely fall back to 'default.png'.

What is the spread operator (...rest) used for in props destructuring?

The rest operator collects all remaining unspecified props into a new object. E.g., function Button({ variant, ...rest }). You can then spread those remaining props directly onto an HTML element: <button className={variant} {...rest}>. This is commonly used to pass down native HTML attributes like disabled, onClick, or aria-labels without manually declaring every single one.

Real-World Example

Forwarding Rest Props in a UI Library: When building design systems, you often want your custom components to accept all standard HTML attributes. By destructuring out the custom props (text, isLoading) and capturing the rest with ...rest, you avoid having to explicitly type out 50 different HTML attributes.

example
jsx
// Building a highly reusable UI button component
function PrimaryButton({ text, isLoading, icon: Icon, ...nativeButtonProps }) {
  return (
    // We spread all remaining props (like disabled, type="submit", onClick, aria-hidden) directly onto the native button
    <button 
      className={`btn-primary ${isLoading ? 'loading' : ''}`}
      disabled={isLoading}
      {...nativeButtonProps} 
    >
      {Icon && <Icon className="mr-2" />}
      {isLoading ? "Loading..." : text}
    </button>
  );
}

// PARENT USAGE
// The parent can pass 'type', 'onClick', and 'aria-label' and they automatically flow through to the native <button> via ...nativeButtonProps
<PrimaryButton 
  text="Submit Payment" 
  isLoading={false} 
  type="submit" 
  onClick={processPayment} 
  aria-label="Pay now"
/>

Check Your Knowledge

Test your understanding of Destructuring Props with these quick questions.

Question 1

What is the correct syntax to destructure `title` and `author` from props?

Question 2

How do you assign a default value to a destructured prop?

Take React Quiz
Outline
React IntroductionSetup & ViteJSX SyntaxVirtual DOMFunction ComponentsComponent TreeReact Fragments
Passing PropsDestructuring PropsChildren PropConditional RenderingList RenderingReact KeysCSS Modules
Event HandlingSynthetic EventsState BasicsuseState HookState BatchingObject StateArray State
useEffect HookDependency ArrayCleanup FunctionsData FetchingStrict Mode
useRef HookPassing Refs (forwardRef)useReducer HookCustom HooksuseId Hook
Prop Drilling ProblemContext APIuseContext HookRedux IntroZustand Alternative
React Router DOMRoute ParametersNested RoutesControlled InputsReact Hook Form
React.memouseMemo HookuseCallback HookCode Splitting (lazy)Bundle Size Optimization
React Server ComponentsClient ComponentsSuspense for Data FetchingNext.js App RouterServer ActionsuseTransition HookReact 19 Forms (useActionState)
React.js

Destructuring Props

Overview

When a component receives data, React automatically bundles all the passed attributes into a single JavaScript object called props. If you pass 5 attributes, you constantly have to write props.title, props.price, props.image, etc., throughout your component. This makes your code unnecessarily wordy and harder to read.

To solve this, React developers universally use JavaScript Object Destructuring directly in the function signature. Destructuring allows you to unpack specific properties from the props object and assign them to local variables immediately as the function receives them.

Not only does this make your JSX much cleaner by removing the repetitive props. prefix, but it also clearly documents exactly which data a component expects right at the very top of the file, making it dramatically easier for other engineers to understand your code.

Syntax

Standard vs Destructured Syntax
jsx
// BAD (Old fashioned): Repetitive props. prefix
function ProductCard(props) {
  return (
    <div>
      <h2>{props.name}</h2>
      <p>{props.price}</p>
      <img src={props.image} alt={props.name} />
    </div>
  );
}

// GOOD (Modern Industry Standard): Destructuring in the signature
function ProductCard({ name, price, image, isSale = false }) {
  return (
    <div>
      {isSale && <span>SALE!</span>}
      <h2>{name}</h2>
      <p>{price}</p>
      <img src={image} alt={name} />
    </div>
  );
}

Common Pitfalls

  • Forgetting the curly braces: If you write function Card(title, desc), React will assign the entire props object to the variable title, and desc will be undefined. You must wrap them in braces to destructure: function Card({ title, desc }).

Interview Questions

How do you provide a default value for a prop that might not be passed by the parent?

You can assign default values directly during destructuring using the equals sign: function User({ avatar = 'default.png', name }). If the parent does not pass an avatar prop, it will safely fall back to 'default.png'.

What is the spread operator (...rest) used for in props destructuring?

The rest operator collects all remaining unspecified props into a new object. E.g., function Button({ variant, ...rest }). You can then spread those remaining props directly onto an HTML element: <button className={variant} {...rest}>. This is commonly used to pass down native HTML attributes like disabled, onClick, or aria-labels without manually declaring every single one.

Real-World Example

Forwarding Rest Props in a UI Library: When building design systems, you often want your custom components to accept all standard HTML attributes. By destructuring out the custom props (text, isLoading) and capturing the rest with ...rest, you avoid having to explicitly type out 50 different HTML attributes.

example
jsx
// Building a highly reusable UI button component
function PrimaryButton({ text, isLoading, icon: Icon, ...nativeButtonProps }) {
  return (
    // We spread all remaining props (like disabled, type="submit", onClick, aria-hidden) directly onto the native button
    <button 
      className={`btn-primary ${isLoading ? 'loading' : ''}`}
      disabled={isLoading}
      {...nativeButtonProps} 
    >
      {Icon && <Icon className="mr-2" />}
      {isLoading ? "Loading..." : text}
    </button>
  );
}

// PARENT USAGE
// The parent can pass 'type', 'onClick', and 'aria-label' and they automatically flow through to the native <button> via ...nativeButtonProps
<PrimaryButton 
  text="Submit Payment" 
  isLoading={false} 
  type="submit" 
  onClick={processPayment} 
  aria-label="Pay now"
/>

Check Your Knowledge

Test your understanding of Destructuring Props with these quick questions.

Question 1

What is the correct syntax to destructure `title` and `author` from props?

Question 2

How do you assign a default value to a destructured prop?

Take React Quiz

React.js coding notes for Indian students — 53 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in React.js

  1. React Introduction
  2. Setup & Vite
  3. JSX Syntax
  4. Virtual DOM
  5. Function Components
  6. Component Tree
  7. React Fragments
  8. Passing Props
  9. Destructuring Props
  10. Children Prop
  11. Conditional Rendering
  12. List Rendering
  13. React Keys
  14. CSS Modules
  15. Event Handling
  16. Synthetic Events
  17. State Basics
  18. useState Hook
  19. State Batching
  20. Object State
  21. Array State
  22. useEffect Hook
  23. Dependency Array
  24. Cleanup Functions
  25. Data Fetching
  26. Strict Mode
  27. useRef Hook
  28. Passing Refs (forwardRef)
  29. useReducer Hook
  30. Custom Hooks
  31. useId Hook
  32. Prop Drilling Problem
  33. Context API
  34. useContext Hook
  35. Redux Intro
  36. Zustand Alternative
  37. React Router DOM
  38. Route Parameters
  39. Nested Routes
  40. Controlled Inputs
  41. React Hook Form
  42. React.memo
  43. useMemo Hook
  44. useCallback Hook
  45. Code Splitting (lazy)
  46. Bundle Size Optimization
  47. React Server Components
  48. Client Components
  49. Suspense for Data Fetching
  50. Next.js App Router
  51. Server Actions
  52. useTransition Hook
  53. React 19 Forms (useActionState)

Study next

  • All coding notes
  • React.js notes home
  • React certification quiz
  • Online compiler
  • Interview Q&A

React.js coding notes for Indian students — 53 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in React.js

  1. React Introduction
  2. Setup & Vite
  3. JSX Syntax
  4. Virtual DOM
  5. Function Components
  6. Component Tree
  7. React Fragments
  8. Passing Props
  9. Destructuring Props
  10. Children Prop
  11. Conditional Rendering
  12. List Rendering
  13. React Keys
  14. CSS Modules
  15. Event Handling
  16. Synthetic Events
  17. State Basics
  18. useState Hook
  19. State Batching
  20. Object State
  21. Array State
  22. useEffect Hook
  23. Dependency Array
  24. Cleanup Functions
  25. Data Fetching
  26. Strict Mode
  27. useRef Hook
  28. Passing Refs (forwardRef)
  29. useReducer Hook
  30. Custom Hooks
  31. useId Hook
  32. Prop Drilling Problem
  33. Context API
  34. useContext Hook
  35. Redux Intro
  36. Zustand Alternative
  37. React Router DOM
  38. Route Parameters
  39. Nested Routes
  40. Controlled Inputs
  41. React Hook Form
  42. React.memo
  43. useMemo Hook
  44. useCallback Hook
  45. Code Splitting (lazy)
  46. Bundle Size Optimization
  47. React Server Components
  48. Client Components
  49. Suspense for Data Fetching
  50. Next.js App Router
  51. Server Actions
  52. useTransition Hook
  53. React 19 Forms (useActionState)

Study next

  • All coding notes
  • React.js notes home
  • React certification quiz
  • Online compiler
  • Interview Q&A