0
0
Reactframework~20 mins

Props destructuring in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Props Destructuring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component?
Consider this React component using props destructuring. What will it render?
React
function Greeting({ name, age }) {
  return <p>Hello, {name}! You are {age} years old.</p>;
}

// Usage: <Greeting name="Alice" age={30} />
A<p>Hello, Alice! You are 30 years old.</p>
B<p>Hello, Alice! You are {age} years old.</p>
C<p>Hello, {name}! You are {age} years old.</p>
D<p>Hello, ! You are 30 years old.</p>
Attempts:
2 left
💡 Hint
Look at how props are unpacked directly in the function parameters.
📝 Syntax
intermediate
2:00remaining
Which option correctly destructures props in a functional component?
You want to extract 'title' and 'content' from props in a React functional component. Which code is correct?
A
function Post(props) {
  return &lt;article&gt;&lt;h1&gt;{props.title}&lt;/h1&gt;&lt;p&gt;{props.content}&lt;/p&gt;&lt;/article&gt;;
}
B
function Post({ title, content }) {
  return &lt;article&gt;&lt;h1&gt;{title}&lt;/h1&gt;&lt;p&gt;{content}&lt;/p&gt;&lt;/article&gt;;
}
C
function Post(title, content) {
  return &lt;article&gt;&lt;h1&gt;{title}&lt;/h1&gt;&lt;p&gt;{content}&lt;/p&gt;&lt;/article&gt;;
}
D
function Post(props) {
  const { title, content } = props;
  return &lt;article&gt;&lt;h1&gt;{title}&lt;/h1&gt;&lt;p&gt;{content}&lt;/p&gt;&lt;/article&gt;;
}
Attempts:
2 left
💡 Hint
Destructuring can happen directly in the function parameters.
🔧 Debug
advanced
2:00remaining
Why does this component throw an error?
This React component tries to destructure props but causes an error. Why?
React
function UserProfile({ user }) {
  const { name, age } = user;
  return <p>{name} is {age} years old.</p>;
}

// Usage: <UserProfile />
ABecause 'name' and 'age' are not valid prop names.
BBecause destructuring must happen in the function parameters, not inside the function body.
CBecause 'user' is undefined when no prop is passed, destructuring 'name' and 'age' fails.
DBecause React components cannot destructure nested objects.
Attempts:
2 left
💡 Hint
Think about what happens if 'user' prop is missing.
state_output
advanced
2:00remaining
What is the output after clicking the button?
This React component uses props destructuring and state. What will it display after clicking the button once?
React
import { useState } from 'react';

function Counter({ initial }) {
  const [count, setCount] = useState(initial);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  );
}

// Usage: <Counter initial={5} />
ACount: 6
BCount: 5
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint
The initial state is set from the 'initial' prop, then incremented by 1 on click.
🧠 Conceptual
expert
2:00remaining
Why prefer props destructuring in React components?
Which is the best reason to use props destructuring in React functional components?
AIt allows React to memoize components without extra code.
BIt improves the performance of React components by reducing rendering time.
CIt automatically validates prop types and prevents runtime errors.
DIt makes the code cleaner and variables are easier to use without repeating 'props.' each time.
Attempts:
2 left
💡 Hint
Think about how destructuring affects code readability.