React · Junior · Question 51 of 75
How do you give a prop a default value in a function component?
function Avatar({ size = 40, alt = "" }) {
return <img width={size} height={size} alt={alt} />;
}- ✓Correct answer: With default values in props destructuring
- 2With
Avatar.defaultProps, the recommended modern way - 3Props can't have defaults; the parent must always pass them
Explanation
Destructuring defaults apply when the prop is undefined. defaultProps for function components was deprecated and removed in React 19.