A. defaultProps is not supported on functional components
B. The component should use destructuring with default values instead
C. No error; this code works correctly
D. The defaultProps assignment should be inside the function
Solution
Step 1: Understand defaultProps on functional components
React supports defaultProps on functional components, so this syntax is valid.
Step 2: Check usage and output
Since no label prop is passed, defaultProps provides 'Click me', so the button shows that text.
Final Answer:
No error; this code works correctly -> Option C
Quick Check:
defaultProps works on functions = No error; this code works correctly [OK]
Hint: defaultProps still works on functions but prefer defaults in parameters [OK]
Common Mistakes:
Thinking defaultProps is deprecated for functional components
Placing defaultProps inside the function body
Confusing defaultProps with state initialization
5. You want a component to have a default prop that is an object with nested values. Which approach correctly sets default props to avoid errors if the prop is missing?
function Profile({ user = { name: 'Guest', age: 0 } }) {
return <div>Name: {user.name}, Age: {user.age}</div>;
}
hard
A. Use destructuring with a default object in the parameter as shown
B. Set defaultProps = { user: { name: 'Guest', age: 0 } } after the function
C. Initialize user inside the function body with if (!user) user = {...}
D. Pass user as a required prop and do not set defaults
Solution
Step 1: Understand default props for nested objects
Setting default nested objects in parameters prevents errors when accessing properties.
Step 2: Compare approaches
Use destructuring with a default object in the parameter as shown uses destructuring with a default object, which safely provides nested defaults.
Final Answer:
Use destructuring with a default object in the parameter as shown -> Option A
Quick Check:
Default nested object in parameters = Use destructuring with a default object in the parameter as shown [OK]
Hint: Set nested defaults in parameter destructuring to avoid undefined errors [OK]
Common Mistakes:
Setting defaultProps with nested objects can cause shallow merge issues
Initializing defaults inside function body can be verbose and error-prone
Not providing defaults leads to runtime errors accessing nested props
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.