This React app shows how to destructure user and age props inside the WelcomeMessage component. It displays a welcome message with the user's name and age.
React
import React from'react';
function WelcomeMessage({ user, age }) {
return (
<div>
<h2>Welcome, {user}!</h2>
<p>Your age is {age}.</p>
</div>
);
}
exportdefaultfunction App() {
return <WelcomeMessage user="Alice" age={30} />;
}
OutputSuccess
Important Notes
If you forget to destructure, you can still use props like props.user, but destructuring is cleaner.
You can also destructure props inside the function body if you prefer.
Destructuring helps with readability and reduces repeated code.
Summary
Props destructuring lets you pick specific props directly in the function parameters.
It makes your React components easier to read and write.
Use it whenever you want cleaner and clearer access to props.
Practice
(1/5)
1. What does props destructuring in a React functional component do?
easy
A. It creates a new component with different props.
B. It extracts specific properties from the props object directly in the function parameters.
C. It changes the state of the component automatically.
D. It prevents the component from receiving props.
Solution
Step 1: Understand props in React
Props are passed as an object to components, containing all passed data.
Step 2: What destructuring does
Destructuring extracts specific keys from the props object directly in the function parameters for easier access.
Final Answer:
It extracts specific properties from the props object directly in the function parameters. -> Option B
Quick Check:
Props destructuring = extracting keys [OK]
Hint: Destructuring picks props by name inside function parentheses [OK]
Common Mistakes:
Thinking destructuring changes state
Confusing destructuring with creating new components
Believing destructuring blocks props
2. Which of the following is the correct syntax to destructure props name and age in a React functional component?
easy
A. function User({name, age}) { return
{name} is {age}
; }
B. function User(props) { const name = props.name; const age = props.age; return
{name} is {age}
; }
C. function User(name, age) { return
{name} is {age}
; }
D. function User(props) { return
{props.name, props.age}
; }
Solution
Step 1: Identify destructuring in parameters
Destructuring happens inside the function parentheses by listing keys in curly braces.
Step 2: Check each option
function User({name, age}) { return <p>{name} is {age}</p>; } correctly destructures name and age directly in the parameters.
Final Answer:
function User({name, age}) { return <p>{name} is {age}</p>; } -> Option A
Quick Check:
Destructuring in parameters = function User({name, age}) { return
{name} is {age}
; } [OK]
Hint: Destructure props inside parentheses with curly braces [OK]
Common Mistakes:
Passing props as separate parameters instead of one object
Manually extracting individual props instead of destructuring
Using commas inside JSX expressions incorrectly
3. What will be the rendered output of this component?
function Greeting({ name, city }) {
return <p>Hello {name} from {city}!</p>;
}
// Usage: <Greeting name="Anna" city="Paris" />
medium
A.
Hello Anna from Paris!
B.
Hello {name} from {city}!
C. Hello Anna from Paris!
D. Error: props not defined
Solution
Step 1: Understand destructuring in parameters
The component extracts name and city from props directly.
Step 2: Render output with given props
Using name="Anna" and city="Paris", the JSX renders as <p>Hello Anna from Paris!</p>.
Final Answer:
<p>Hello Anna from Paris!</p> -> Option A
Quick Check:
Destructured props render correct text [OK]
Hint: Destructured props show passed values in JSX [OK]
Common Mistakes:
Expecting literal {name} text instead of value
Forgetting to pass props when using component
Confusing JSX tags with plain text
4. Identify the error in this React component using props destructuring:
B. Destructuring syntax is incorrect in the function parameters.
C. Using props.description inside the component causes an error.
D. JSX tags are not properly closed.
Solution
Step 1: Check destructuring usage
The function destructures title and description from props, so props is not defined inside.
Step 2: Identify incorrect usage
Using props.description causes an error because props is not declared; should use description directly.
Final Answer:
Using props.description inside the component causes an error. -> Option C
Quick Check:
Destructuring means no props object inside function [OK]
Hint: Use destructured names directly, not props.name [OK]
Common Mistakes:
Using props.property after destructuring
Thinking destructuring creates a new props object
Ignoring error messages about undefined variables
5. You want to create a React component that shows a user's firstName, lastName, and age. You receive props as a single object. Which is the best way to use props destructuring to make your code clean and readable?
hard
A. function User(props) { const { firstName, lastName, age } = props; return
{firstName} {lastName} is {age} years old.
; }
B. function User(firstName, lastName, age) { return
{firstName} {lastName} is {age} years old.
; }
C. function User() { return
{props.firstName} {props.lastName} is {props.age} years old.
; }
D. function User({ firstName, lastName, age }) { return
{firstName} {lastName} is {age} years old.
; }
Solution
Step 1: Compare destructuring styles
function User(props) { const { firstName, lastName, age } = props; return <p>{firstName} {lastName} is {age} years old.</p>; } destructures inside the function body, which works but is less concise.
Step 2: Identify best practice
function User({ firstName, lastName, age }) { return <p>{firstName} {lastName} is {age} years old.</p>; } destructures props directly in the function parameters, making code cleaner and easier to read.
Step 3: Check incorrect options
function User() { return <p>{props.firstName} {props.lastName} is {props.age} years old.</p>; } uses props without declaring it, causing error. function User(firstName, lastName, age) { return <p>{firstName} {lastName} is {age} years old.</p>; } treats props as separate parameters, which is invalid.
Final Answer:
function User({ firstName, lastName, age }) { return <p>{firstName} {lastName} is {age} years old.</p>; } -> Option D
Quick Check:
Destructure props in parameters for clean code [OK]
Hint: Destructure all needed props inside function parentheses [OK]
Common Mistakes:
Destructuring inside function body instead of parameters
Using props without declaring it
Passing props as separate parameters instead of one object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.