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} />;
}

Explanation

Destructuring defaults apply when the prop is undefined. defaultProps for function components was deprecated and removed in React 19.

Further reading

Problem with a question

React · Junior · question 51

Test yourself

Take the React · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz